Results 1 to 2 of 2
Thread: Thread v swing timer
- 06-23-2011, 09:34 PM #1
Member
- Join Date
- May 2011
- Posts
- 11
- Rep Power
- 0
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.
-
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
-
help with Swing Timer animation
By tomas1991 in forum New To JavaReplies: 7Last Post: 03-19-2010, 09:06 AM -
Swing Timer Questions
By morfasto in forum New To JavaReplies: 9Last Post: 11-14-2009, 03:48 PM -
Inside a Timer thread loop,how to refresh a JTable in swing
By neha_negi in forum Threads and SynchronizationReplies: 3Last Post: 09-04-2009, 01:45 AM -
Timer on swing
By finzaiko in forum AWT / SwingReplies: 3Last Post: 04-02-2009, 07:45 AM -
Thread vs Timer
By dawiz001 in forum Threads and SynchronizationReplies: 5Last Post: 03-07-2009, 08:25 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks