About swing Timer and synchronized



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
.



Relevant Pages

  • Re: System.Timers.Timer performance test ?
    ... > Each USer object create a MyTimer object. ... > public delegate void MyTimerHandler; ... > private DateTime startTime, endTime; ... > private int name; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Data Object Collection Inheritance
    ... > private System.Windows.Forms.ListBox listBox1;> private System.Windows.Forms.Button cmdID;> private System.Windows.Forms.Button cmdText; ... > private void InitializeComponent() ... > // TODO: Add BaseDataCollection.IsReadOnly getter implementation> return false; ... > public sealed class SubClassCollection: ...
    (microsoft.public.dotnet.languages.csharp)
  • RE: Problem in converting to C#
    ... public delegate void delegate1 ... private void InitializeComponent() ... private void Button1_Click(object sender, System.EventArgs e) ... Private Sub Form1_Load(ByVal sender As System.Object, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Law of Demeter can be supported without eliminating object coupling
    ... void C::M ... // do something to change solution state ... or there is a disconnect here as well. ...
    (comp.object)
  • Re: GPS Library - Listed Here
    ... > public string Longitude_Sexagesimal ... > private States state; ... > public StatusType GpsStatus ... > public void Start ...
    (microsoft.public.dotnet.framework.compactframework)