InterruptedException using FileSystemView without other swing components

风流意气都作罢 提交于 2021-01-28 01:31:43

问题


I'm getting frustrated trying to get rid of this pesky exception that displays when my Java program completes:

Exception while removing reference: java.lang.InterruptedException java.lang.InterruptedException at java.lang.Object.wait(Native Method) at java.lang.ref.ReferenceQueue.remove(Unknown Source) at java.lang.ref.ReferenceQueue.remove(Unknown Source) at sun.java2d.Disposer.run(Unknown Source) at java.lang.Thread.run(Unknown Source)

The source of my woes is the use of the FileSystemView class. My code is adapted from code on this page, using File.listRoots() and then the FileSystemView to display friendly names/descriptions of my Windows drives. A simplified version of the code which runs fine, but leaves daemon threads running which cause problems, is this:

File[] roots = File.listRoots();
FileSystemView fsv = FileSystemView.getFileSystemView();
for (File f : roots) {
    System.out.format("%s (%s) %d/%d%n", fsv.getSystemDisplayName(f), fsv.getSystemTypeDescription(f),
            f.getUsableSpace(), f.getTotalSpace());
}

Use of the FileSystemView appears to start up some daemon threads (Java2D Disposer, AWT-Windows, Swing-shell, etc.) that cause the issue.

Running that code by itself seems to work fine. Running it in association with a lot more windows resources using JNA causes the problem to occur much more regularly. A full example of the problematic code can be found by executing the test class here, on Windows.

I've searched high and low for answers and have hints at things to try, but nothing seems to work (yet). For reference, I've looked at these two threads, among others, which describe my symptoms but don't provide helpful results:

  • Occasional InterruptedException when quitting a Swing application
  • InterruptedException after cancel file open dialog - 1.6.0_26

The first link above discusses daemon vs. non-daemon threads. I've confirmed that the "main" thread, which is reaching the end of the program when this issue occurs, is the only non-daemon thread running. Both threads above, and many other sources, refer to disposing of other swing components as a solution, but the only "swing" code I'm using is the FileSystemView, and I don't see any options for disposing of it.

Questions:

  • Is there a way to nicely shutdown the daemon threads that swing starts when using the FileSystemView?
  • Is there a way I can easily "wrap" my FileSystemView object inside a container (never displayed to the user) that I can then easily dispose() of, to possibly answer the previous question?
  • Is there an alternative to FileSystemView that gives a rich description of windows drive names and volume names (e.g., A: (Floppy Drive), D: (CD-Rom), etc.) that avoids starting swing daemon threads that seem to cause these problems?

回答1:


I suspect that something to do with the FileSystemView requires the code to be executed on the Even Dispatch Thread - the thread used for event dispatching & processing in rich client desktop apps.

See Concurrency in Swing for more details.



来源:https://stackoverflow.com/questions/32367929/interruptedexception-using-filesystemview-without-other-swing-components

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!