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

isInterrupted() or interrupted() !!!!

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have a look at this code below..
public class TestThread extends Thread{
private String mesg="";
public void run(){

while(!isInterrupted()) {
try {
sleep(1000);
}
catch(InterruptedException e){
}
System.out.println(mesg);
}

}
public TestThread(String m) {
mesg = m;
setName(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();
System.out.println("Hey");
}
Which is better for a check in "while"
isInterrupted or interrupted()
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
Corey
 
If you two don't stop this rough-housing somebody is going to end up crying. Sit down and read this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic