Abort one thread inside another thread start method in Java

I read this post , and the sentences that interrupted one stream from another,

"" Here are a few approaches that should work if they are implemented correctly.

You could like threads regularly check some common flag variable (for example, call it stopNow), and arrange for both threads to set it when they are done. (The flag variable must be mutable ... or correctly synchronized.)

You can have threads regularly call the Thread.isInterrupted () method to see if it was interrupted. Then each thread should call Thread.interrupt () on the other when it ends. ""

I do not understand how a second approach is possible that uses Thread.isInterrupted() . That is, how Thread-1 Thread.interrupt() on Thread-2 .



Consider this example, in the main method, I run two threads t1 and t2 . I want t1 stop t2 after reaching a certain condition. how can i achieve this

  class Thread1 extends Thread { public void run(){ while (!isDone){ // do something } } //now interrupt Thread-2 } class Thread2 extends Thread { public void run(){ try { while(!Thread.isInterupted()){ //do something; } catch (InterruptedExecption e){ //do something } } } public class test { public static void main(String[] args){ try { Thread1 t1 = new Thread1(); Thread2 t2 = new Thread2(); t1.start(); t2.start(); } catch (IOException e) { e.printStackTrace(); } } } 
+1
source share
4 answers
 public class test { private static boolean someCondition = true; public static void main(String[]args){ Thread t2 = new Thread(new someOtherClass("Hello World")); Thread t1 = new Thread(new someClass(t2)); t2.start(); t1.start(); try { t1.join(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } static class someClass implements Runnable{ Thread stop; public someClass(Thread toStop){ stop = toStop; } public void run(){ while(true){ try { Thread.sleep(500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(someCondition && !stop.isInterrupted()){ stop.interrupt(); } } } } static class someOtherClass implements Runnable{ String messageToPrint; public someOtherClass(String s){ messageToPrint = s; } public void run(){ while(true){ try { Thread.sleep(500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(messageToPrint); } } } 

}

+2
source

The context is that you are trying to implement your circuit using thread interrupts.

For this to happen, the t1 object needs a reference to the stream object t2 , and then it just calls t2.interrupt() .

There are many ways in which t1 can get a link to t2 .

  • It can be passed as a constructor parameter. (You need to create an instance of Thread2 before Thread1 ...)
  • It can be installed by calling the setter in Thread1.
  • It can be retrieved from a static variable or array or from any one β€œregistry” object.
  • This can be found by listing all threads in the ThreadGroup, looking for one that matches the name t2 .
+2
source

You may consider using the Future interface. It provides a cancel() method. http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Future.html

0
source

Playing intermittently makes your life unnecessarily difficult. Besides the fact that your code needs to know the threads, the interrupt does not provide any contextual information about the cause of the interrupt.

If you have a condition that is shared by your code, possibly executed by different threads, just encapsulate this condition in an object and share this object:

 public class Test { public static void main(String[] args) { Condition c=new Condition(); new Thread(new Setter(c)).start(); new Thread(new Getter(c, "getter 1")).start(); // you can simply extend it to more than one getter: new Thread(new Getter(c, "getter 2")).start(); } } class Getter implements Runnable { final Condition condition; final String name; Getter(Condition c, String n) { condition=c; name=n; } public void run() { while(!condition.isSatisfied()) { System.out.println(name+" doing something else"); try { Thread.sleep(300); } catch(InterruptedException ex){} } System.out.println(name+" exiting"); } } class Setter implements Runnable { final Condition condition; Setter(Condition c) { condition=c; } public void run() { System.out.println("setter: doing my work"); try { Thread.sleep(3000); } catch(InterruptedException ex){} System.out.println("setting condition to satisfied"); condition.setSatisfied(); } } class Condition { private volatile boolean satisfied; public void setSatisfied() { satisfied=true; } public boolean isSatisfied() { return satisfied; } } 

The great advantage of this encapsulation is that it is easy to expand. Suppose you want to allow a thread to wait for a condition, rather than poll it. Taking the code above is easy:

 class WaitableCondition extends Condition { public synchronized boolean await() { try { while(!super.isSatisfied()) wait(); return true; } catch(InterruptedException ex){ return false; } } public synchronized void setSatisfied() { if(!isSatisfied()) { super.setSatisfied(); notifyAll(); } } } class Waiter implements Runnable { final WaitableCondition condition; final String name; Waiter(WaitableCondition c, String n) { condition=c; name=n; } public void run() { System.out.println(name+": waiting for condition"); boolean b=condition.await(); System.out.println(name+": "+(b? "condition satisfied": "interrupted")); } } 

Without changing other classes, you can now expand your test case:

 public class Test { public static void main(String[] args) { WaitableCondition c=new WaitableCondition(); new Thread(new Setter(c)).start(); new Thread(new Getter(c, "getter 1")).start(); // you can simply extend it to more than one getter: new Thread(new Getter(c, "getter 2")).start(); // and you can have waiters new Thread(new Waiter(c, "waiter 1")).start(); new Thread(new Waiter(c, "waiter 2")).start(); } } 
0
source

All Articles