• 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

isInterrupted() method vs interrupted() method

 
Ranch Hand
Posts: 34
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know basically what these methods do...! But when I run the following programming it is giving result and that confused me a lot.

first i will post code:

//interrupting thread not in sleep or waiting state...!


In the above program , after thread interruption for the below code, I am getting output as: false & true.

-------------------------------------------------------------------------------------------------------------------
      t1.interrupt();  
System.out.println("is thread interrupted..:"+t1.interrupted());
System.out.println("is thread interrupted..:"+t1.isInterrupted());

----------------------------------------------------------------------------------------------------------------

My question is, after interruption how the interrupted() method giving result as false
but isInterrupted() method is giving result as true.....?


Please help me in this...?

thanks in advance...!
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch. Have you looked at the API documentation of the methods in class Thread? It explains the difference.

The documentation for interrupted() states:

Tests whether the current thread has been interrupted. The interrupted status of the thread is cleared by this method.


And the documentation for isInterrupted() states:

Tests whether this thread has been interrupted. The interrupted status of the thread is unaffected by this method.


So, the interrupted() method tells you if the thread is interrupted, but also clears the interrupted status of the thread. The isInterrupted() method just tells you if the thread is interrupted, but does not change the interrupted status of the thread.

Also, very important: the interrupted() method is a static method. It does not check if the thread you are calling it on is interrupted, but if the current thread is interrupted. A call like t1.interrupted() is misleading - it does not check if thread t1 is interrupted, but if the main thread is interrupted!
 
kiran madhan
Ranch Hand
Posts: 34
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh..! Thanks a lot ...! I understood what was my mistake...thanks a lot...Jesper de Jong..!
 
Men call me Jim. Women look past me to this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic