Win a copy of React Cookbook: Recipes for Mastering the React Framework this week in the HTML Pages with CSS and JavaScript forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

is interrupt call work only once for a thread?

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here in this code i used interrupt call two time from main thread to interrupt child thread but when i executed it only once my catch block run.

class MyThread extends Thread{
public void run(){
for(int i=0;i<10;i++){
System.out.println("child thread");
try{
Thread.sleep(2000);
}
catch(InterruptedException e){
System.out.println("i got interrupted");
}
}
}
}
class ThreadInterruptDemo{
public static void main(String[] args){
MyThread t=new MyThread();
t.start();
t.interrupt();
System.out.println("end of main");
t.interrupt();
}
}
 
Greenhorn
Posts: 5
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That works very like a flag {@code interrupted = true}, but not like a counter. You need to wait until interrupted flag is cleared, and then the thread can be interrupted repeatedly.
 
Marshal
Posts: 73738
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SG: welcome to the Ranch and thank you for a helpful post.
 
Yeah, but is it art? What do you think tiny ad?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic