• Post Reply 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

suspend and resume method - Thread

 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Friends,
What is the result of compiling and executing the following Java class:

public class ThreadTest extends Thread {
public void run() {
System.out.println("In run");
suspend();
resume();
System.out.println("Leaving run");
}
public static void main(String args []) {
(new ThreadTest()).start();
}
}
A> Compilation will fail in the method main.
B> Compilation will fail in the method run.
C> A warning will be generated for method run.
D> The string "In run" will be printed to standard out.
E> Both strings will be printed to standard output.
F> Nothing will happen.
Select the most appropriate answer.
The ans. given is D. I agree with it but resume() method should also play its part and hence E ption should be the ans.
Please correct me if I am wrong.
Jyotsna

 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jyotsna
The resume() comes after the suspend(). After the suspend() is applied the thread is suspended and needs some outside stimulus to make it resume. A suspended thread can't resume itself, it would be like trying to wake yourself up if your sleeping.
Here is the quote from the Thread class in the API:

If the thread is alive, it is suspended and makes no further progress unless and until it is resumed.


So it would never reach the second print statement unless some outside method called resume on it. These two methods are also deprecated in favor of wait() and notify(). Here's the explaination form the API

Why are Thread.suspend and Thread.resume deprecated?
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. Such deadlocks typically manifest themselves as "frozen" processes.


hope that helps

Dave
[This message has been edited by Dave Vick (edited June 28, 2001).]
 
Jyotsna Umesh
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Dave for providing relevant info.
Jyotsna
 
And tomorrow is the circus! We can go to the circus! I love the circus! We can take this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic