File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Thread Issues when Using a Timer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Thread Issues when Using a Timer" Watch "Thread Issues when Using a Timer" New topic
Author

Thread Issues when Using a Timer

Kevin Simonson
Ranch Hand

Joined: Oct 22, 2011
Posts: 97
Let's say I have a class that extends {JPanel} that tracks the progress of a vehicle. It has stored a velocity for that vehicle, and I use {javax.swing.Timer} to redraw the display each 100 milliseconds, say, with the vehicle having moved as far across the display as the velocity would have it move. But I also want to implement {MouseListener} so that I can increase or decrease that velocity with the click of my mouse. Do I need to synchronize access to this velocity just in case the timer goes off at the same time the user is clicking on the mouse, in order to prevent two threads from accessing the velocity at the same time and potentially corrupting the data? If so, what can I synchronize on? Should I just forget about using {javax.swing.Timer} and do the whole thing with two threads, one that handles the mouse clicks and one that periodically updates the vehicle's position?

Kevin S
Kevin Simonson
Ranch Hand

Joined: Oct 22, 2011
Posts: 97
Kevin Simonson wrote:Let's say I have a class that extends {JPanel} that tracks the progress of a vehicle. It has stored a velocity for that vehicle, and I use {javax.swing.Timer} to redraw the display each 100 milliseconds, say, with the vehicle having moved as far across the display as the velocity would have it move. But I also want to implement {MouseListener} so that I can increase or decrease that velocity with the click of my mouse. Do I need to synchronize access to this velocity just in case the timer goes off at the same time the user is clicking on the mouse, in order to prevent two threads from accessing the velocity at the same time and potentially corrupting the data? If so, what can I synchronize on? Should I just forget about using {javax.swing.Timer} and do the whole thing with two threads, one that handles the mouse clicks and one that periodically updates the vehicle's position?

Here's an example:

So my question is, does the fact that {mousePressed()} and {actionPerformed()} both have
unsynchronized access to {velocity} raise the risk of corrupting the value of that variable? And if
so, how would I synchronize that access?

Kevin S
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18108
    
  39


The javax.swing.Timer callbacks, and the swing event callbacks, all use the event-dispatching-thread. In effect, what you are describing is single threaded.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread Issues when Using a Timer
 
Similar Threads
Got a small keybinding bug
pause within the mouseReleased method
OneToOne mapping problem
Adding a progress bar to an otherwise non-gui app
Swing timer question.