Forums Register Login

suspend and resume method - Thread

+Pie Number of slices to send: Send
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

+Pie Number of slices to send: Send
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).]
+Pie Number of slices to send: Send
Thanks Dave for providing relevant info.
Jyotsna
It's exactly the same and completely different as this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1019 times.
Similar Threads
mock exam questions -
threads
mock exam questions -
Thread
suspend()
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 15:20:50.