Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
}
}
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Plese do not multipost. Responses here please.
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic