Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

using threads in swing

 
Suman Mummaneni
Ranch Hand
Posts: 96
Android Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I am a swing programmer. I want to know if I can use threads in my swing application that I am developing. I also want to know if I can make some classes abstract/intreface so that in future i could use them while programming.

Cheers!
Suman
 
Joe Ess
Bartender
Posts: 9300
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Java Tutorial: Building a GUI with Swing: How to use Threads should answer your first question.
As for your second question:

I also want to know if I can make some classes abstract/intreface so that in future i could use them while programming.

What makes you think that you couldn't?
 
Dave Mace
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you can definitely use threads. In fact, anything that is going to take some time to process (i.e. database reads, i/o activity, etc) you should consider moving the processing of those tasks to another thread.

The problem if you don't is that they block your Swing Event Thread from processing any GUI activity which results in a perceived application lockup to the end-user because the GUI will not respond until the offending method call finishes and unblocks the EVT (event thread).

Make sense?
 
Jason Kingsley
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The essential thing to remember with threads is the magic paradigm - "never update a control on a thread other than the event thread". This means that you have to manage access to controls from any worker thread.

Take a look at http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html for some useful information and pay attention to the utility class SwingWorker. This class makes life a little easier for managing calls across threads to the event thread.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic