• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

Regarding Threads

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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


 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic