Hello,
I would like your comment about synchronized and
SwingUtilities.invokeLater in this example with javax.swing.Timer:
/*
* First
*/
import java.awt.event.*;
import javax.swing.*;
public class MyTimer{
private Timer timer;
public MyTimer(){
initTimer();
}
public void startTimer(){
timer.start();
}
public static void main(String[] args) {
MyTimer myTimer = new MyTimer();
myTimer.startTimer();
JOptionPane.showMessageDialog( null, "Wait...","MyTimer" ,
JOptionPane.WARNING_MESSAGE);
}
private void initTimer(){
timer = new Timer( 5000, new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
doAction();
}
} );
}
private void doAction(){
System.out.println("Hello!");
}
}
///////////////////////////////////////////////////////////////////////
/*
* Second
*/
.....
public static void main(String[] args) {
MyTimer myTimer = new MyTimer();
myTimer.startTimer();
JOptionPane.showMessageDialog( null, "Wait...","MyTimer" ,
JOptionPane.WARNING_MESSAGE);
}
......
private synchronized void doAction(){
System.out.println("Hello!");
}
/////////////////////////////////////////////////////////////
/*
* Third
*/
......
public static void main(String[] args) {
MyTimer myTimer = new MyTimer();
myTimer.startTimer();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JOptionPane.showMessageDialog( null, "Wait...","MyTimer" ,
JOptionPane.WARNING_MESSAGE);
}
});
}
.......
private void doAction(){
System.out.println("Hello!");
}
//////////////////////////////////////////////////////////////
/*
* Fourth
*/
......
public static void main(String[] args) {
MyTimer myTimer = new MyTimer();
myTimer.startTimer();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JOptionPane.showMessageDialog( null, "Wait...","MyTimer" ,
JOptionPane.WARNING_MESSAGE);
}
});
}
.......
private synchronized void doAction(){
System.out.println("Hello!");
}
Thanks,
Paolo
Daniel Pitts - 15 Nov 2007 22:23 GMT
> Hello,
> I would like your comment about synchronized and
> SwingUtilities.invokeLater in this example with javax.swing.Timer:
[snipped code]
First, use EventQueue.invokeLater instead.
javax.swing.Timer calls the actionPerformed on the EDT. Its generally a
bad idea to use synchronize on the EDT, because you might block it for
an arbitrarily long time, and make the GUI unresponsive.
To learn more, about thread safety, I suggest reading the book Java
Concurrency In Practice.
<http://virtualinfinity.net/wordpress/technical-book-recommendations/java-concurr
ency-in-practice/>
As well as the sun Swing tutorials related to threading.
There are plenty of ways to pass information to and from the event
dispatch thread (or any other single thread) that are preferable over
using synchronize.
Hope this helps,
Daniel.

Signature
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
marpauser@gmail.com - 16 Nov 2007 07:07 GMT
> First, use EventQueue.invokeLater instead.
>
[quoted text clipped - 18 lines]
> --
> Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
Thank you.
I have a more complex application with a swing Timer that correctly
starts,
but after a long time it doesn't fires its event: I never stop Timer
and there is a check that says that Timer "isRunning()" and
"isRepeats()".
The problem is synchronized ?
Seems that also without synchronized problem stays.
Hello,
Paolo
Roedy Green - 15 Nov 2007 22:58 GMT
>I would like your comment about synchronized and
>SwingUtilities.invokeLater in this example with javax.swing.Timer:
There is no need for synchronized with javax.swing.Timer. It never
invokes more than one event at a time. Further, System.out.println is
thread safe.

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com