• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

About suspend() and resume() in Thread

 
debasish tripathy
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx a lot for your kind help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic