2

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:

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?
Community
  • 1
  • 1
Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
  • For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Sep 03 '15 at 05:58
  • 2
    BTW - I suspect that something to do with the FSV requires the code to be executed on the Even Dispatch Thread. See [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) for more details. – Andrew Thompson Sep 03 '15 at 06:02
  • You've tried wrapping the code in a try/catch block? – Craig Sep 03 '15 at 06:03
  • Andrew is probably correct, try a `SwingWorker` while executing that section. – Craig Sep 03 '15 at 06:05
  • @AndrewThompson I tried to create a minimal example, as you see quoted above, unfortunately the (intermittent) error only occurs with a much larger example. Thanks for the pointer to Event Dispatch Thread. – Daniel Widdis Sep 03 '15 at 15:01
  • @Craig the exception occurs when program is trying to exit at completion, so it's impossible to catch. I'll try the SwingWorker. – Daniel Widdis Sep 03 '15 at 15:04
  • 1
    @AndrewThompson your solution is spot on. I wrapped the FSV inside a swingworker, execute()d it, and used get() for the results. Please post in "Answer" section so I can accept your answer. – Daniel Widdis Sep 03 '15 at 16:06
  • @DanielWiddis glad I could help – Craig Sep 03 '15 at 19:13

1 Answers1

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.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • This did the trick. I used a SwingWorker thread with the FSV inside it, executing and then getting the results, and it solves my problem. Thanks! – Daniel Widdis Sep 03 '15 at 20:17