Search code examples
javaswingswingworker

Will this code run in UI thread?


For example, My UI thread execute a SwingWorker thread, called as A. In the done() function of A thread, it create a SwingWorker thread B. Now, the code in the done() function of thread B is running on UI thread or thread A?

It make sense that, the code should be running on thread A, but if I don't call get() function in thread A, thread A will not wait for thread B and thread A probably will finish its task. If thread B now execute the code in done() function, thread A is not there already. Then how the code be handled in this case?


Solution

  • As per the SwingWorker API documentation, SwingWorker#done() runs on the Swing event dispatch thread (EDT). This is true for both done() methods that you mention above. You know that there's an easy way to find out by using the SwingUtilities.isEventDispatchThread() method.