Threadsafe Timers?

L

larkmore

I have recently begun the joys of ensuring that all of my GUI/Swing
code is thread safe by adding invokeLater() and invokeAndWait() methods
everywhere. As far as I can tell, all Swing classes are assumed to NOT
be thread safe. Does this include javax.swing.Timer? Specifically, I
use Timers all over the place for regularly scheduled tasks and when I
turn them on or off, or restart() them, do these also need to be
enclosed in invokeLater() and invokeAndWait() methods? Is it safe to
assume that the event handlers for Timers execute on the event
dispatching thread so that anything within these tasks is safe without
the extra protection? In case it matters my target JVM is the 1.3.1_07
JRE from Sun. Thanks for any help!
-Will
 
T

Thomas Weidenfeller

Is it safe to
assume that the event handlers for Timers execute on the event
dispatching thread so that anything within these tasks is safe without
the extra protection?

Well, tell me, what is your interpretation of the following quote from
the Timer API documentation?

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.

/Thomas
 
J

John C. Bollinger

I have recently begun the joys of ensuring that all of my GUI/Swing
code is thread safe by adding invokeLater() and invokeAndWait() methods
everywhere. As far as I can tell, all Swing classes are assumed to NOT
be thread safe. Does this include javax.swing.Timer? Specifically, I
use Timers all over the place for regularly scheduled tasks and when I
turn them on or off, or restart() them, do these also need to be
enclosed in invokeLater() and invokeAndWait() methods? Is it safe to
assume that the event handlers for Timers execute on the event
dispatching thread so that anything within these tasks is safe without
the extra protection? In case it matters my target JVM is the 1.3.1_07
JRE from Sun. Thanks for any help!

From the API docs for javax.swing.Timer:

"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."

John Bollinger
(e-mail address removed)
 
F

Filip Larsen

Will wrote
I have recently begun the joys of ensuring that all of my GUI/Swing
code is thread safe by adding invokeLater() and invokeAndWait() methods
everywhere. As far as I can tell, all Swing classes are assumed to NOT
be thread safe. Does this include javax.swing.Timer? Specifically, I
use Timers all over the place for regularly scheduled tasks and when I
turn them on or off, or restart() them, do these also need to be
enclosed in invokeLater() and invokeAndWait() methods? Is it safe to
assume that the event handlers for Timers execute on the event
dispatching thread so that anything within these tasks is safe without
the extra protection? In case it matters my target JVM is the 1.3.1_07
JRE from Sun. Thanks for any help!

Even though you use 1.3 you might find the documentation for the 1.4
javax.swing.Timer a bit more helpful. Let me quote from
http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/Timer.html:

"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."

"In v 1.3, another Timer class was added to the Java platform:
java.util.Timer. Both it and javax.swing.Timer provide the same basic
functionality, but java.util.Timer is more general and has more
features. The javax.swing.Timer has two features that can make it a
little easier to use with GUIs. First, its event handling metaphor is
familiar to GUI programmers and can make dealing with the
event-dispatching thread a bit simpler. Second, its automatic thread
sharing means that you don't have to take special steps to avoid
spawning too many threads. Instead, your timer uses the same thread used
to make cursors blink, tool tips appear, and so on." -- End of quote.



Regards,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top