Win a copy of Terraform in Action this week in the Cloud forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Threads

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class InterruptTest implements Runnable {
>
> public static void main( String [] args ) throws Exception {
> Thread sleepyThread = new Thread( new InterruptTest() );
> sleepyThread.start();
>
> Thread.sleep(500);
> sleepyThread.interrupt();
> Thread.sleep(500);
> sleepyThread.interrupt();
> }
>
> public void run() {
> while ( true ) {
> try {
> System.out.println("Sleeping...");
> Thread.sleep( 1000*1000 );
> } catch ( InterruptedException e ) {
> System.out.println("interrupted...");
> }
> }
> }
>}
>
>After call to the interrupt() method in main(), i.e. after Interrupted
>Exception, it again starts executing the while loop. Can anyone explain what does interrupt() does ?
>
Another question: What is the difference b/w wait(), yield() & sleep()? In which of the 3 cases, the thread releases the monitor?
>Thanks
kanchan
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good question. I know for sure that wait() will cause a thread to give up the monitor, and that sleep() will NOT cause a thread to give it up.
However, I am unsure about yield(). I think that a thread that yields will not give up a monitor. After all, if threadA has the monitor, and the JVM schedules threadB for execution, threadA does not lose the monitor. So, why should a yielded thread give the monitor?
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kanchan!
Your program is preety good example of interrupt().
The actual process occured like follows:
1. In main() sleepyThread created and started.
2. Execution starts in run() method print "Sleeping..." and then sleep for 1000 seconds.
3. While in main(), The Main Thread sleeps for 500 millisecs and then interrupts the sleepyThread which cause sleepyThread to throw InterruptedException.
4. So sleepyThread throw exception which caught and then afterwards "interrupted...." print, because this run structure is in while loop which always true so loop starts again print "Sleeping....".
5. While in main thread sleep(500) takes place.
6. after printing in sleepyThread sleep(1000*1000) takes place again which again interrupted by main thread as "sleepyThread.interrupt()" so again exception caught and then print message "interrupted..." and then again loop starts and print "Sleeping...".
7. main thread ends execution.
8. sleepThread sleeps for 1000 seconds which is not interrupted by main thread anymore so for 1000 seconds program stops and then print "Sleeping..." again.
I think you follow the execution path. Now come to your second question.


Another question: What is the difference b/w wait(), yield() & sleep()? In which of the 3 cases, the thread releases the monitor?


The answer is :
1. wiat() takes parameters as milliseconds or milliseconds and nanosecs. It results the thread to stop execution and go to waiting state but release it's resources and locks.
2. While sleep() takes same type of parameters same thing that it stop the execution of thread and send it to waiting pool but does'nt release resources or lock.
3. Yield() is different that this results for a thread to go to ready state from running state and allow other threads, which have the same priority as the thread which start yield() method, to enter in running state.
I think all above points clear to you
Tell me if i'm wrong
Regards
Farhan
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
The explanation for yield has to be modified a bit. Calling yield on a thread can make the thread give up its locks. If there are other threads in ready to run state, then the JVM might give them a chance to execute, but if there are no such threads, then almost immediately the thread that yielded, will go back to running state and reacquire its locks.
Whether some other thread should be given a chance to execute would depend on the JVM implementation. For example, it might be dictated by the thread priority for some implementations of the JVM.
So calling yield does not guarantee that the thread will always give up its locks. This is different from the behaviour of wait().
Cheers,
Sajida
 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Friends,
First - while loop will never end, because while(true) is an infinite loop.
Second - calling interrupt method does't terminate the run method because in this program while infinite loop is running and the try block is just fine, no interruption, so no Interrupted Exception is thrown.
What JLS is says - An interrupt request is posted for this thread. This thread does not necessarily react immediately to the interrupt, however. If this thread is waiting, it is awakened and it then throws an InterruptedException.
Please correct me if I am wrong.
Thanks & regards,
V.Srinivasan
[This message has been edited by V Srinivasan (edited June 11, 2001).]
[This message has been edited by V Srinivasan (edited June 11, 2001).]
 
kanchan chaudhary
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi friends,
What I have found out is that executing the interrupt() method interrupts the current thread temporarily. Then, that interrupted thread moves to the ready state. & the next time it begins executing, it executes the InterruptedException.
Any corrections welcome.
Thanks
kanchan
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic