• Post Reply Bookmark Topic Watch Topic
  • New Topic

suspend and resume method - Thread  RSS feed

 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
Thanks Dave for providing relevant info.
Jyotsna
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!