Synchronizing on a passed object, JFrame is freezing

I created JFrame which contains and InternalFrame which draws figures which are moving(each figure is another Thread) I wanted to pauseButtonmake it pause, so I wanted to synchronize them on the passed object.

But when I hit the pause button the whole window is freezing and I cannot hit the play button Another thing is that only one is running at the time, I want them all running then all pause.

 class A extends JFrame{

    ....
    Object o = new Object();
    JButtton pauseButton = new JButton("pause");
   JButtton playButton = new JButton("play");
    B b = new B(o);
        pauseButton.addActionListener(new ActionListener() {    
            @Override
            public void actionPerformed(ActionEvent arg0) {
                synchronized (synchronizator) {
                    try {
                        synchronizator.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }   
                }

            }
        });
        playButton.addActionListener(new ActionListener() { 
            @Override
            public void actionPerformed(ActionEvent arg0) {
                synchronized (synchronizator) {
                    synchronizator.notifyAll(); 
                }

            }
    ...
    }

class B extends JInternalFrame{
Object o;
B(Object o){this.o = o}
./...

many... C thread = new C(o);

....
}

    class C extends Thread{
    Object o;
    booolean running;
    public void run(){
    while(running){
       synchronized(o){


       }
    }
    }

}

Answers


Noooo!! ;)

All Swing activity should be done on the AWT Event Dispatch Thread (EDT). Use normal thread-agnostic objects and perhaps javax.swing.Timer (not java.util!) for timing.

You may want to do other things not involving Swing on different threads, but I suggest keeping a really clean separation. That is to say very few objects should be dealing with thread issues.

If you are using the bare low-level Java synchronisation facilities, set a condition prior to notify/notifyAll and put your waits within while loops.


Need Your Help

Hierarchical data from DB - to join or not to join

c# asp.net sql database hierarchical-data

I am trying to find a clean and performant solution for this problem, but am stuck somehow.

Mobile application with intel xdk about rmote database

php intel

I am developing a mobile application which has social feature and database driven application. I am done registration and login system with PHP, mysql. But I want to put posting system and want to ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.