• 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:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Code confusion

 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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

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.
 
Ben Zaidi
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, it really clarified the confusion.
 
Ben Zaidi
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
No thanks. We have all the government we need. This tiny ad would like you to leave now:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic