Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2007

Tip: Looking for answers? Try searching our database.

About swing Timer and synchronized

Thread view: 
marpauser@gmail.com - 15 Nov 2007 21:50 GMT
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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.