This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Code confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Code confusion" Watch "Code confusion" New topic
Author

Code confusion

Ben Zaidi
Ranch Hand

Joined: Apr 08, 2007
Posts: 151


This is an example from the sun's site. I have almost understood the example, but i am confused at line 68. As t is interrupted, it means
the run() method will exit immediately. But what does line 68 signifies in this code? I understood the usage of t.join(1000) at line
58 which means that, currently stop the main thread for and wait for 1 second for MessageLoop to complete. If it doesn't just interrupt the
thread t. But what does line 68 means? Because after throwing the InterruptedException thread t will terminate and main will complete its
execution. Any pointers will be helpful.


Ben,
--Lost in preparation of SCJP and SCWCD--
"Start writing a new chapter, for if you live by the book you'll never make history." (Ben Sobel)
Anastasia Sirotenko
Ranch Hand

Joined: Jul 20, 2009
Posts: 64
Ben Zaidi wrote:
As t is interrupted, it means the run() method will exit immediately. But what does line 68 signifies in this code? I understood the usage of t.join(1000) at line
58 which means that, currently stop the main thread for and wait for 1 second for MessageLoop to complete. If it doesn't just interrupt the
thread t. But what does line 68 means? Because after throwing the InterruptedException thread t will terminate and main will complete its
execution. Any pointers will be helpful.

when you call t.interrupt, the interrupted flag will be set, but the thread will be interrupted only if something is watchig this flag. The Thread.sleep(4000); construction in run() method does watch the interrupded flag, so the t thread will be interrupded as soon as it tryies to execute next Thread.sleep(4000);. Between t.interrupt() and Thread.sleep(4000); the main thread teoretically can do something, so if you want to prevent this, you do t.join() after you set t.interrupt() just to wait while the thread will be fully interrupted.
in this particular program you can get this output if comment out t.join():

and the usage of t.join garantees this output:

And then, if you remove all Thread.sleep() construction with its InterruptedException catch block and include the for loop in some while(true) loop, the t thread will never terminate, just because nothing will check if the interrupted flag is set for the thread.


[SCJP 6.0]
Ben Zaidi
Ranch Hand

Joined: Apr 08, 2007
Posts: 151
Thanks, it really clarified the confusion.
Ben Zaidi
Ranch Hand

Joined: Apr 08, 2007
Posts: 151
Hello,

It was really helpful. But i want to know how sleep() method is related in checking the status of the Interrupt flag.
My question is, does only sleep() method does that or some other method from Thread class also does that like
wait() or something else. If sleep is related, then how it checking the status of interrupted flag?
Anastasia Sirotenko
Ranch Hand

Joined: Jul 20, 2009
Posts: 64
Ben Zaidi wrote:Hello,

It was really helpful. But i want to know how sleep() method is related in checking the status of the Interrupt flag.
My question is, does only sleep() method does that or some other method from Thread class also does that like
wait() or something else. If sleep is related, then how it checking the status of interrupted flag?


As fas as i know Thread.sleep() and Thread.wait() methods check interrupted flag of a thread and throw InterruptedException if it is set.
But still you can use Thread.interrupted() (it returns true if thread is interrupted and resets the flag to false) and Thread.isInterrupted() (it returns true if thread is interrupted and doesnt reset anything) methods to control your thread's state by yourself.
 
Don't get me started about those stupid light bulbs.
 
subject: Code confusion
 
Similar Threads
Thread,Join problem
Dan's Thread
Interrupt Handling
t.join() in Sun's thread example
Threads..join() method