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

interrupting a thread

 
Ranch Hand
Posts: 318
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can somebody tell me when does the static method interrupted() returns true
i am trying hard to print it
isInterrupted() works fine though
confused??

------------------
KS
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
interrupted() is a static method of Thread class. interrupted() checks whether the currently executing thread is interrupted or not. When you say t.check(), you are calling interrupted() from within the context of the main thread. Thus you are checking whether the main thread has been interrupted or not.
If you replace interrupted() with isInterrupted(), it should print out true & work as you expected, since isInterrupted() is an instance method.
interrupted() should generally be called from within the run() method of a thread. It checks whether the currently executing thread has been interrupted or not, and it immediately clears the interrupted status flag of the thread. An example of this would be:

Hope that helps!

[This message has been edited by Junaid Bhatra (edited February 11, 2001).]
 
Kalpesh Soni
Ranch Hand
Posts: 318
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
nop!
evenif i print interrupted() in the exception handler of InterruptedException it does not print true ??!!
------------------
KS
 
Ranch Hand
Posts: 1871
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
kalpesh soni,
This discussion will be more appropriately handled in the Thread and Synchronization forum. I request the moderators here to to moved it there.
 
Junaid Bhatra
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kalpesh,
As soon as InterruptedException is thrown, the interrupted status flag of the thread is set to false. Thus if you check interrupted() or isInterrupted() within the catch block of InterruptedException, it will always return false (unless ofcourse another thread has again called interrupt() on this thread).
Also the difference between interrupted() and isInterrupted() is that calling interrupted() clears the interrupted status flag of the thread, while isInterrupted() does not.
 
Kalpesh Soni
Ranch Hand
Posts: 318
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know that interrupted status will be cleared when InterruptedException is thrown
and the static interrupted() method works on the thread which is calling it
so junaid,can you tell me, when interrupted() will return true ?
can you right some ACTUAL code so that i can see it being printed as true??
please

------------------
"Sun Certified Java Programmer"
KS
"Failing to plan is like plannig to fail!"
 
Junaid Bhatra
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kalpesh,
If you see my post above, I already wrote the code skeleton using which interrupted() would print true. Anyways I've modified that to write some ACTUAL code that prints true when interrupted() is called.

This gives the output:
true
interrupted() has returned true!!!
Regards,
Junaid

[This message has been edited by Junaid Bhatra (edited February 13, 2001).]
 
Kalpesh Soni
Ranch Hand
Posts: 318
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow
thanks for replying junaid
I was trying to interrupt the thread while it was waiting or sleeping so it always ended up with an exception
I did not want to offend you in anyway
thanks again

------------------
"Sun Certified Java Programmer"
KS
"Failing to plan is like plannig to fail!"
 
Junaid Bhatra
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No problem.
Happy coding!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic