File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Synchronization practice: why does thread1 rush through loop without pause or reset? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Synchronization practice: why does thread1 rush through loop without pause or reset?" Watch "Synchronization practice: why does thread1 rush through loop without pause or reset?" New topic
Author

Synchronization practice: why does thread1 rush through loop without pause or reset?

Nigel Shrin
Ranch Hand

Joined: May 18, 2009
Posts: 140

This is a program I've written to understand the 'interrupt' primarily, and synchronisation in general:




The t1 output never pauses per second, and never gets the random interrupt, and it always completes the 20 iterations.
The t2 thread pauses per second, and gets the "random reset".

Can anyone explain the behaviour please?
Many thanks.


Nigel
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

I think, it is the job of the interrupt() method. Interruption is a mechanism whereby a thread that is waiting (or sleeping) can be made to prematurely stop waiting. I'm not sure about that execution of all sleep() method will be interrupted!

Let's see, what other do!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
geeta vemula
Ranch Hand

Joined: Jul 18, 2008
Posts: 208
yes it is by interrupt call on t1 thread.

See java docs where it is clearly given
"If this thread is blocked in an invocation of the wait(), wait(long), or wait(long, int) methods of the Object class, or of the join(), join(long), join(long, int), sleep(long), or sleep(long, int), methods of this class, then its interrupt status will be cleared and it will receive an InterruptedException".

Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Yea. But, one doubt : is interrupt() method comes to action, when every time that particular thread goes to sleep/blocked mode? Please clear my doubt. Here, the Thread t1 goes to sleep every time the method cube is being invoked on the Thread and it doesn't stay in that sleep state. is this because of interrupt() method called on that thread?

Thanks in Advanced!
geeta vemula
Ranch Hand

Joined: Jul 18, 2008
Posts: 208
yes it is coming in action everytime it is going to sleep mode.
Larry Chung
Ranch Hand

Joined: Feb 02, 2010
Posts: 247
Nigel Shrin wrote:


The t1 output never pauses per second, and never gets the random interrupt, and it always completes the 20 iterations.
The t2 thread pauses per second, and gets the "random reset".

Can anyone explain the behaviour please?
Many thanks.


BTW, different platforms have different Thread Schedulers, which give different results. I executed the Sleep_Interrupt code on a Mac OS X with JRE 6 and did not get the t2 thread pause per second and did not get the "random resets".


SCJP 6
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
Abimaran Kugathasan wrote:Yea. But, one doubt : is interrupt() method comes to action, when every time that particular thread goes to sleep/blocked mode? Please clear my doubt. Here, the Thread t1 goes to sleep every time the method cube is being invoked on the Thread and it doesn't stay in that sleep state. is this because of interrupt() method called on that thread?

Call to interrupt() sets 'interrupted' flag of the thread.
Then when sleep() is called, this flag is checked and if this flag is true, sleep() aborts immediately with InterruptedException and this flag is cleared.
Your code catch this exception, and in the catch block this flag is set to true again, so next invocation of sleep() will throw InterruptedException again.
Nigel Shrin
Ranch Hand

Joined: May 18, 2009
Posts: 140
Thank you Larry, its interesting to see how very different the result is on a Mac.

Thank you Ireneusz, that appears to be the case. I have added a little more output to prove the point:



(on WinXP) for every iteration of t1 it gives the output


etc

For every iteration of t2 "line 22" is read:



This appears to mean that the execution of main processes the interrupt before the t1.start goes into run()?

Thank you all for your contributions

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Synchronization practice: why does thread1 rush through loop without pause or reset?