About swing Timer and synchronized

Discussion in 'Java' started by marpauser, Nov 15, 2007.

  1. marpauser

    marpauser Guest

    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
     
    marpauser, Nov 15, 2007
    #1
    1. Advertisements

  2. marpauser

    Daniel Pitts Guest

    [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.
     
    Daniel Pitts, Nov 15, 2007
    #2
    1. Advertisements

  3. marpauser

    Roedy Green Guest

    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.
     
    Roedy Green, Nov 15, 2007
    #3
  4. marpauser

    marpauser Guest

    First, use EventQueue.invokeLater instead.
    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
     
    marpauser, Nov 16, 2007
    #4
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.