pause and resume

Hi,

I have a Thread that has a pause and resume method. I also have 2 Jbuttons that call those two methods.

When I press pause, the program hangs.

Thanks in advance for your help.

public void uPause() {

try {

synchronized(this) {

this.wait();

}

} catch (InterruptedException e) {

}

}

public void wakeUp () {

synchronized(this) {

this.notify();

}

}

[457 byte] By [rvy2k] at [2007-9-26 13:56:17]
# 1
It is recommended to go with notifyAll() in almost all cases. So you should give it a shot.
thpreusser at 2007-7-2 15:00:30 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 2
thanks but the my problem is that i can't pause...
rvy2k at 2007-7-2 15:00:30 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 3
I hope you do not pause directly inside the ActionHandler, i.e. the actionPerformed() method. If so, you are blocking Swing's event dispatch thread, which then looks like a hanging application as no more GUI events are handled.
thpreusser at 2007-7-2 15:00:30 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 4
well in my action performed i have a if (event.equals("pause") then call the method in the thread that is pausing the program.is that bad?
rvy2k at 2007-7-2 15:00:30 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 5

It's not only bad, it is the source of your problem! The thread you are blocking on the wait() is Swing's event handler, which you actually also want to use to unblock the waiting thread but it simply can't since it's blocked. You are robbing Swing's capability of handling GUI events, which essentially looks like a hanging application as lond as every action needs to be triggered by the user.

thpreusser at 2007-7-2 15:00:30 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 6

When designing Swing applications, you should always give serious thought to using threads for the application itself so as to decouple the application from the GUI.

What you have done there is actually block the event thread which kills your entire app.

Think of a swing application in terms of layers (something that is encouraged with the MVC nature of Swing). The outer layer is your GUI, the middle layer(s) are application components and lower layers are things like databases and so on.

The GUI is used to pass messages to the application layer. The term message here is used deliberately and you should not confuse it with method calls.

In your application you want to pause the application layer so you have to have some way of passing this "pause" message to it. If your app layer runs in its own thread (recommended for anything serious) then you can do something like this...

In your pause action event handler you do the following...

SwingUtilities.invokeLater(new Runnable() {

public void run() {

synchronized(MyApp.this) {

try {

MyApp.this.wait();

} catch(InterruptedException ie) {

}

}

}

});

In your wake up action event handler you do this

SwingUtilities.invokeLater(new Runnable() {

public void run() {

synchronized(MyApp.this) {

MyApp.this.notify();

}

}

});

SwingUtilties.invokeLater(Runnable); causes the Runnable object to be placed in the event queue and returns immediately. The Runnable object is then processed at the next available opportunity with the event thread itself. In practical terms this is immediately however some delay can occur if lots of events are being handled, this delay is never usually serious.

What does all this get you? Well, you won't be blocking the event thread in Swing, you have decoupled the Application layer from the GUI layer and have full control over the thread(s) you use to run the applications in.

Using SwingUtilities.invokeLater(Runnable) is -the- only correct way to perform tasks like this from within event handlers. SwingUtilities.invokeAndWait(Runnable) can also be used but the need to wait for the Runnable to complete is rare and invokeAndWait(Runnable) can be thorny to use.

andyba at 2007-7-2 15:00:30 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 7

One of the thorns that WILL ***** you with invokeAndWait(Runnable) in the event dispatcher thread is that using it will cause the dispatcher thread to come to a sudden halt. The dispatcher will be waiting on the Runnable to complete however it will never be able to actually invoke it because it is waiting for the Runnable to complete.

If you didn't understand that don't worry, just never use invokeAndWait(Runnable) directly in the event dispatcher thread.

andyba at 2007-7-2 15:00:30 > top of Java-index,Java HotSpot Virtual Machine,Specifications...