Results 1 to 6 of 6
  1. #1
    Samgetsmoney is offline Member
    Join Date
    Feb 2009
    Posts
    32
    Rep Power
    0

    Default Question on swing timers

    lets say i have a bunch of swing timers like this:

    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
    		}
    	});
    Does it create a new thread for each timer or does it all run on one thread?
    If it does create a new thread for each thats very bad right? So what is an alternative?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    No, it does not create a new thread except once. Read the API on Swing Timers and you'll see the answer.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    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.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    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

  1. Question mark colon operator question
    By orchid in forum Advanced Java
    Replies: 9
    Last Post: 12-19-2010, 08:49 AM
  2. JFrame/Swing Question
    By Moncleared in forum AWT / Swing
    Replies: 9
    Last Post: 01-16-2009, 08:16 PM
  3. Replies: 0
    Last Post: 12-31-2008, 02:20 PM
  4. Swing Question, can you help?
    By Silentstormz in forum AWT / Swing
    Replies: 4
    Last Post: 09-21-2008, 03:02 AM
  5. map javax.swing.text.Element to javax.swing.text.View
    By elizabeth in forum New To Java
    Replies: 1
    Last Post: 07-30-2007, 07:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •