Results 1 to 6 of 6
Thread: Question on swing timers
- 02-19-2009, 11:38 PM #1
Member
- Join Date
- Feb 2009
- Posts
- 32
- Rep Power
- 0
Question on swing timers
lets say i have a bunch of swing timers like this:
Does it create a new thread for each timer or does it all run on one thread?Java Code:static javax.swing.Timer a = new javax.swing.Timer(500, new ActionListener() { public void actionPerformed(ActionEvent e) { //do something } }); static javax.swing.Timer b = new javax.swing.Timer(300, new ActionListener() { public void actionPerformed(ActionEvent e) { //do something } }); static javax.swing.Timer c = new javax.swing.Timer(1000, new ActionListener() { public void actionPerformed(ActionEvent e) { //do something } });
If it does create a new thread for each thats very bad right? So what is an alternative?
-
No, it does not create a new thread except once. Read the API on Swing Timers and you'll see the answer.
- 02-20-2009, 02:24 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
All the timers are running on the same thread, UI thread you are working on with them.
-
Their actionPerformed method runs on the EDT, or Event Dispatch Thread, but the timing or waiting portion runs on a single background thread. Per the API:
Although all Timers perform their waiting using a single, shared thread (created by the first Timer object that executes), the action event handlers for Timers execute on another thread -- the event-dispatching thread. This means that the action handlers for Timers can safely perform operations on Swing components. However, it also means that the handlers must execute quickly to keep the GUI responsive.
- 02-20-2009, 05:59 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Hmm, I think it's run on same UI thread. Can't we use it on the same thread anyway. Just a try actually, if I want to avoid use of more threads.
- 02-20-2009, 07:34 AM #6
As is very clear from the API quoted by Fubarable, only two Threads are involved no matter how many Timers you use. The waiting Thread, and the EDT.
If the code in the actionPerformed is likely to block the EDT, it should be launched in a separate background Thread (or by using a Swing Worker's doInBackground). In this respect, it is no different from any task launched from the EDT.
db
Similar Threads
-
Question mark colon operator question
By orchid in forum Advanced JavaReplies: 9Last Post: 12-19-2010, 08:49 AM -
JFrame/Swing Question
By Moncleared in forum AWT / SwingReplies: 9Last Post: 01-16-2009, 08:16 PM -
Concurrent timers are not getting invoked for same timer info (Serializable object co
By Neeraj in forum Enterprise JavaBeans (EJB)Replies: 0Last Post: 12-31-2008, 02:20 PM -
Swing Question, can you help?
By Silentstormz in forum AWT / SwingReplies: 4Last Post: 09-21-2008, 03:02 AM -
map javax.swing.text.Element to javax.swing.text.View
By elizabeth in forum New To JavaReplies: 1Last Post: 07-30-2007, 07:02 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks