my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Regarding Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Regarding Threads" Watch "Regarding Threads" New topic
Author

Regarding Threads

Joseph Fernando
Greenhorn

Joined: Oct 31, 2000
Posts: 4
I know that this question relates to Suspend, Resume which
has been deprecated and does not figure in the JCP exam but
I came across this code in a mock exam.
class Threadex extends Thread
{
public void run()
{
System.out.println(" before suspends");
suspend();
resume();
System.out.println(" after resume");
}
public static void main(String args[])
{
Threadex a = new Threadex();
Thread t = new Thread(a);
t.start();
}
}
According to me the output for this code should be
'before suspends'
and the program should get suspended.
But in reality the output is
'before suspends'
'after resume' and the program terminates.
Could someone explain how do i suspend the 'Thread t'.

Thanks
Joseph
prasanthi kothapa
Ranch Hand

Joined: Oct 19, 2000
Posts: 30
hi joseph,
the thread has only been suspended, it goes into the wait()
state.hence on calling resume() it comes back to the ready
state,it continues from where it got suspended.

hope it helps
bye ..
prasanthi
Joseph Fernando
Greenhorn

Joined: Oct 31, 2000
Posts: 4
Hi Prasanthi,
Thanks for your view. But the doubt persists in my mind
as to how can the thread call resume on itself when
it is suspended.
Thanks
Joseph
Originally posted by prasanthi kothapa:
hi joseph,
the thread has only been suspended, it goes into the wait()
state.hence on calling resume() it comes back to the ready
state,it continues from where it got suspended.

hope it helps
bye ..
prasanthi

Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
Joseph,
Think of these methods as signalling operations which shout for the attention of thread scheduler. A thread object is just like any other object, but it communicates with the thread scheduler( a native program which is vendor dependent )
When you call <code>suspend() ( or resume() or wait() or notify() or notifyAll() or stop() or interrupt() </code> etc ) internally a flag is set indicating the state change. The "big-brother" (aka thread scheduler ) who is watching all the members of his family( all reachable thread objects ) makes note of this state change and initiates an appropriate action - like removing the thread from the running pool and adding it to the waiting pool, or the other way around as the situation may be.
It is really important to remember merely calling these methods will not guarantee an immediately observable behaviour. Since everything depends on the thread scheduler you should look at all of these transitional methods as state-setters. And when you do, a suspended thread calling <code>resume()</code> on itself isn't really such a big deal. It is just another plain-vanilla method call!!
HTH
Ajith


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Sasikanth Malladi
Ranch Hand

Joined: Nov 04, 2000
Posts: 175
Guys, I agree with Joseph. I ran a program exactly like this one (which I too got from a mock exam) and it produced "before suspends" and crashed ( I had to hit Ctrl+C) to get back the command line.
And logical explanations?
Shashi
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Regarding Threads