aspose file tools*
The moose likes Beginning Java and the fly likes sleep() vs suspend()  and  stop()  vs  volatile Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic
Author

sleep() vs suspend() and stop() vs volatile

amit sanghai
Ranch Hand

Joined: Dec 05, 2000
Posts: 231
I am confused between sleep and suspend. It is written in the site "http://java.sun.com/products/jdk/1.2/docs/guide/misc/threadPrimitiveDeprecation.html" that suspend causes deadlock. But sleep is the same thing. So, it should also cause deadlock.
1) The following para is written in the site:
"Thread.suspend is inherently deadlock-prone. If the target thread holds a lock on the monitor protecting a critical system resource when it is suspended, no thread can access this resource until the target thread is resumed. If the thread that would resume the target thread attempts to lock this monitor prior to calling resume, deadlock results".
Does it mean that if the thread that is holding the lock is suspended, before some other thread locks the object it has to call resume().
2) Stopping a thread causes it to unlock all the monitors that it has locked. They have said to use "volatile" variable to indicate that the thread can be stopped.
private volatile Thread blinker;

public void start() {
blinker = new Thread(this);
blinker.start();
}
/*public void stop() {
blinker.stop(); // UNSAFE!
}*/
public void stop() {
blinker = null; // correct
}
public void run() {
Thread thisThread = Thread.currentThread();
while (blinker == thisThread) {
try {
thisThread.sleep(interval);
} catch (InterruptedException e){
}
repaint();
}
}
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Plese do not multipost. Responses here please.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: sleep() vs suspend() and stop() vs volatile