Question

I'm currently attempting to debug a medium scale (in the 10's of thousands of lines ballpark) Java project which uses both JavaFX and Swing, and I'm hitting some odd exceptions every so often which I'm pretty sure are because of UI code not being called on the correct thread. The stack trace for these exceptions isn't really helpful at all, since they pretty much all originate from the UI drawing thread.

Now, sure I could sit down with a toothcomb and debug every UI call until I find one that's not being called on the correct thread, and keep doing that for the entire project, but that would be an incredibly long task. Is there some form of easier way to do this sort of debugging? For instance, somehow cause UI code to print out a debug message or throw an exception when it's not been called from its appropriate thread?

Was it helpful?

Solution

Running JavaFX and Swing on the same thread might help fix your threading issues.

There is an experimental feature in Java 8 to run JavaFX and Swing on the same thread:

https://javafx-jira.kenai.com/browse/RT-30694 http://bugs.sun.com/view_bug.do?bug_id=8015477

I think -Djavafx.embed.singleThread=true is the command line property setting to enable the experimental single threading system.

I am not sure if the experimental feature is available in the current Java 8 builds, but I think it might be now, so you may wish to try it.

If you need more information on the experimental single threading feature, you could ask the developers on the openjfx-dev mailing list.


Java 8 has better inbuilt reporting of when code is not run on the correct thread, it's not comprehensive, but it might assist you in locating the source of your error, even if you are not using the single threading option.


Some other users running large applications merging Swing and JavaFX reported similar hard to debug threading issues, so you could check those threads to see if your issues have the same cause.

OTHER TIPS

You could turn on thread checks in glass by -Dglass.disableThreadChecks=false. This would switch on the thread checks in the lowest layer of JavaFX which is responsible for working with OS level APIs. In most cases those checks would be sufficient, because most of the calls are ending up in Glass. These checks would be enabled by default soon.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top