Results 1 to 2 of 2
  1. #1
    musico is offline Member
    Join Date
    May 2011
    Rep Power

    Default Thread v swing timer

    I have seen this distinction a lot and really don't get what it is. For doing simple animation in java swing, it seems accepted to use swing timer instead of threads. But what is the difference? If you use the ActionListener or ActionEvent, is that a thread?

    If anyone knows a good tutorial on this that would be good as well as any I've found so far have been rubbish.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power


    A Swing Timer uses a background thread to allow you to call code repeatedly with a specified time period of repetition. If constructed and used correctly, all code within the Timer's actionPerformed method will be performed on the main Swing thread, the EDT, and this makes using a Swing Timer an easy way for Swing applications to call code repeatedly in this fashion. A disadvantage is the same as the advantage: that all code is called on the EDT, and so if you have a very time/CPU intensive bit of code, it could bog down the Swing thread and thus bog down the percieved responsiveness of your Swing GUI application.

    A background thread can also be used to do repeated tasks by having it hold a loop and Thread.sleep(...) but the caveat here is that care must be taken to be sure that almost all Swing calls are performed on the event dispatch thread, lest you risk an intermittent pernicious concurrency error that could crash the application.

    Which you choose is up to you. Myself, I'd start with the Swing Timer since it is easier to use and less likely to crash your application.

Similar Threads

  1. help with Swing Timer animation
    By tomas1991 in forum New To Java
    Replies: 7
    Last Post: 03-19-2010, 09:06 AM
  2. Swing Timer Questions
    By morfasto in forum New To Java
    Replies: 9
    Last Post: 11-14-2009, 03:48 PM
  3. Inside a Timer thread loop,how to refresh a JTable in swing
    By neha_negi in forum Threads and Synchronization
    Replies: 3
    Last Post: 09-04-2009, 01:45 AM
  4. Timer on swing
    By finzaiko in forum AWT / Swing
    Replies: 3
    Last Post: 04-02-2009, 07:45 AM
  5. Thread vs Timer
    By dawiz001 in forum Threads and Synchronization
    Replies: 5
    Last Post: 03-07-2009, 08:25 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