File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes isInterrupted() or interrupted() !!!! 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 "isInterrupted() or interrupted() !!!!" Watch "isInterrupted() or interrupted() !!!!" New topic

isInterrupted() or interrupted() !!!!

Murgan Sub

Joined: Feb 06, 2002
Posts: 22
Have a look at this code below..
public class TestThread extends Thread{
private String mesg="";
public void run(){

while(!isInterrupted()) {
try {
catch(InterruptedException e){

public TestThread(String m) {
mesg = m;

public static void main(String[] args){

TestThread t1=new TestThread("good");

TestThread t2=new TestThread("goodhey");

TestThread t3=new TestThread("goodheyhey");

t1.start(); t1.interrupt();
t2.start(); t2.interrupt();
t3.start(); t3.interrupt();
Which is better for a check in "while"
isInterrupted or interrupted()
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Murgan Sub:
Which is better for a check in "while"
isInterrupted or interrupted()

For this particular example, it probably won't make much difference, but there are some subtle differences between the Thread methods interrupted() and isInterrupted().
First of all, the method interrupted() is a static method. More importantly, however, the interrupted() method clears the interrupted status of the thread. Therefore, if a thread was interrupted, calling interrupted() once would return true, while a second call to it would return false. The isInterrupted() method, on the other hand is neither static nor does it affect the interrupted status of the thread.
You'll have to think about how you'd like your application to run to determine which you'd prefer to use.

SCJP Tipline, etc.
I agree. Here's the link:
subject: isInterrupted() or interrupted() !!!!
It's not a secret anymore!