File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic

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 "" 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);
/*public void stop() {
blinker.stop(); // UNSAFE!
public void stop() {
blinker = null; // correct
public void run() {
Thread thisThread = Thread.currentThread();
while (blinker == thisThread) {
try {
} catch (InterruptedException e){
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.
I agree. Here's the link:
subject: sleep() vs suspend() and stop() vs volatile
jQuery in Action, 3rd edition