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