About swing Timer and synchronized

M

marpauser

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
 
D

Daniel Pitts

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-concurrency-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.
 
R

Roedy Green

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

marpauser

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/j...>

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.

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
 

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,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top