Bookmark Topic Watch Topic
  • New Topic

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

 
amit sanghai
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Report post to moderator
Plese do not multipost. Responses here please.
 
    Bookmark Topic Watch Topic
  • New Topic