• Post Reply Bookmark Topic Watch Topic
  • New Topic

Swing Threading issues

 
Mu Lan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
In some situations, I need to perform a relatively time consuming task, such as saveChanges() before closing a JFrame or JDialog. If I let a worker thread to take care of the task, then the Swing thread is going to execute dispose(). Then the worker thread won't complete. The reason I want to use a worker thread is so that I can come back to the Swing thread and update the status bar, etc. The way the user knows what's going on. Without using the worker thread, the UI looks unresponsive while the application is completing the task.
Thanks,
-Mu Lan
 
Ashwin Desai
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
In fact, the worker thread will complete execution even if the frame is disposed. But, if you mark the thread as daemon, then on a System.exit() in the main thread, the main thread as well as any other threads are terminated.
If you want the UI Frame to be open while the worker thread completes execution, consider using a synchronizing mechanism like a Barrier to communicate between the main thread and the worker.
for e.g.
Pseudo-Code :
When user presses exit,
1) Spawn a worker thread to do the save operation.
2) Wait for the worker thread to complete (could use workerThread.join() .. Not sure though)
3) call frame.dispose();
Hope this helps.
Ashwin.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!