The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes  please explain me interrupt(), isInterrupted(), interrupted()  method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark " please explain me interrupt(), isInterrupted(), interrupted()  method" Watch " please explain me interrupt(), isInterrupted(), interrupted()  method" New topic

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

Sadaf Haroon

Joined: Jan 04, 2002
Posts: 3
plz explain me the method of interrupt(),isInterrupted(),interrupted()
Rob Ross

Joined: Jan 07, 2002
Posts: 2205
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
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.

SCJP 1.4
Cindy Xu

Joined: Apr 15, 2002
Posts: 6
thank Rod for such detailed explanation. It helps me a lot.
Pralay Misra

Joined: Sep 29, 2013
Posts: 1
Thanks Rob. Its really helpful.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 44000
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.
Have you checked out Aspose?
subject: please explain me interrupt(), isInterrupted(), interrupted() method
It's not a secret anymore!