File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes About suspend() and resume() in Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "About suspend() and resume() in Thread " Watch "About suspend() and resume() in Thread " New topic
Author

About suspend() and resume() in Thread

debasish tripathy
Greenhorn

Joined: Jun 22, 2000
Posts: 8
Hi everybody,
A new comer to this forum.Got the following doubt.
Which one statement below is true concerning the following application?
1. class TestThread2 extends Thread {
2. public void run() {
3. System.out.println("Starting");
4. yield();
5. resume();
6. System.out.println("Done");
7. }
8.
9. public static void main(String args[]) {
10. TestThread2 tt = new TestThread2();
11. tt.start();
12. }
13. }
I found this question in a mock test exam.I don't remember the name of this exam.
The answer given is both the "Starting" and "Done" will be printed.
But i think that only "Starting" will be printed because after the thread tt is suspended the resume will have no effect and it will remain in the hanging position until you stop the application by pressing ^C.
Pls clear my doubt.Thanking all of you in advance.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
i got your doubt,
yield() is a static method of thead class.when ever yield() is called ,thread changes its state from running to ready. when thread was scheduled again by CPU it will change its state to running .
yield()
RUNNING----------->READY
READY ------------>RUNNING
****************************************************
suspend() is instance method of thread class,it has no control on its own ,but some other thread has to call resume method on this thread.until some thread is not called it will be in blocked state.when ever resume() ic called it will change its state to ready state.
suspend()
RUNNING STATE----------------->(SUSPENDED)BLOCKED state

resume()
(SUSPENDED)BLOCKED STATE---------------->READY STATE

READY---------->RUNNING
when ever thread was allowed by CPU ,it will change from ready to running state
hope this clears you
suresh
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Same problem two weeks ago.
Lets talk a Walk thru the code.
1. starting with the run() method
2. the System class used system.out to print "Starting"
3. the Thread class static method yield() move the thread from a running state to a ready state. When the JVM determine that there are resources available to run the thread in question. the JVM on it own, will start running the thread.
4. the depreciated Thread class method resume() does nothing.
5. the System class used system.out to print "Done"
6. the run() method ends
hope this helps, Monty6
[This message has been edited by monty6 (edited June 22, 2000).]
debasish tripathy
Greenhorn

Joined: Jun 22, 2000
Posts: 8
Thanx a lot for your kind help.
 
Consider Paul's rocket mass heater.
 
subject: About suspend() and resume() in Thread