This week's book giveaway is in the Design forum.
We're giving away four copies of Head First Software Architecture: A Learner's Guide to Architectural Thinking and have Raju Gandhi, Mark Richards, Neal Ford on-line!
See this thread for details.
  • 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

please explain me interrupt(), isInterrupted(), interrupted() method

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
plz explain me the method of interrupt(),isInterrupted(),interrupted()
 
Bartender
Posts: 2205
  • Likes 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's start with isInterrupted(). This method returns true if the internal interrupted flag is set for the thread, or false otherwise.
When a Thread is first created, it has never been interrupted, so calling isInterrupted() will return false. If the Thread goes along its merry way and is never interrupted, then calling isInterrupted() will never return true. Ok, this was the easy part.
When you want to interrupt a thread, you need to have a reference to it, ie, a variable that holds a reference to the thread you want to interrupt. Say I have a variable myThread of type Thread, that contains a reference to the thread I want to interrupt. I can now interrupt that thread by calling
myThread.interrupt();
What does this do to the called thread? Like I mentioned above, this sets the internal interrupted flag for the thread. Now if I have code in the myThread Thread that calls isInterrupted(), it will return true.
Now, it starts getting a little more tricky. If myThread is running a loop and not explicitly calling sleep() or wait(), then it must check for the interrupted status by polling the isInterrupted() method continuously. For example:

If we don't check the isInterrupted() result, then we will never know if we were interrupted or not. Also, note the interrupt mechanism is a tool for you to use to design your software-what you actually do after being interrupted is up to your algorithm and specific to your program. It's just a way for one Thread to tap another Thread on the shoulder and say "excuse me Mr. Thread..."; after that it's up to you do something with that interruption.
Now, the other method, interrupted() is similar to isInterrupted() in that it returns the true/false status of the interrupted flag, EXCEPT it also clears the flag, so if you call interrupted() and it returns true, the very next time you call interrupted() it will return false, untill your thread is again interrupted. This method is useful when you are going to be interrupted a lot and you want to make sure you only handle each interruption one time, with a specific method for example.
Finally, there are 2 methods that are special when dealing with interrupts, namely wait() and sleep(). If a Thread is blocked, either because it is sleeping, or waiting, then it's not actively running, and so it can't actively respond to an interrupt from another thread. So the designers of java provided a mechanism whereby if you interrupt a Thread that is asleep, or waiting for a lock, it can be "woken up" to handle the interruption. That's why you must wrap calls to sleep() or wait() in a try/catch block and catch the InterruptedException.
Hope this helps.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank Rod for such detailed explanation. It helps me a lot.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rob. Its really helpful.
 
Marshal
Posts: 79475
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

I presume you have found the Java™ Tutorials section about concurrency. Rob Ross' answer tell you things not in the tutorials, and it is nice to see that old threads are still useful.
 
The overall mission is to change the world. When you've done that, then you can read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic