0

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){


       }
    }
    }

}
Yoda
  • 17,363
  • 67
  • 204
  • 344
  • `which draws figures which are moving(each figure is another Thread)` please why, are those thread connecting Database, RMI, Socket, read FileIO ???, if not then use only one Swing Timer as already suggested by @Tom Hawtin - tackline, tons code example here about put all (prepared) Objects to arrays and inside paintComponent to pick_up Objects from array based on whatever conditions – mKorbel May 02 '13 at 11:48

1 Answers1

2

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.

Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305
  • and reason why SwingWorker is implemented in official API – mKorbel May 02 '13 at 11:20
  • @mKorbel What do you mean by that? `SwingWorker` has been in the Java library for years. However, I wouldn't recommend it as unnecessarily ties your code together. – Tom Hawtin - tackline May 02 '13 at 11:23
  • Could you please show how use that timer if I have a ActionListener in class A and I want to pause `run()` in class C.(main post) – Yoda May 02 '13 at 11:23