Possible duplicate: How do you kill a thread in Java?
I need to stop a large task by sending an interrupt signal to Thread. I use most of the APIs from java.util.concurrent. *. My task is to send to Thread and execute. This task comes from the client, so I do not control this code.
The task is similar to:
public class Task1 extends Thread { public void run() { while(true){ if(Thread.interrupted()){ return; } for(int i=0; i<Integer.MAX_VALUE; i++){ System.out.println("I am task 1 " + i); } } } };
I want to basically stop the loop through the for loop when it receives the interrupt signal (note that I cannot put the Thread.interrputed () logic inside the loop because it comes from the client.) I have another class that use the Executor to complete this task.
public class ConcurrentTest { public static void main(String[] args) {
}
Task 2:
public class Task2 extends Thread{ public void run() { while(true){ if(Thread.interrupted()){ return; } System.out.println("I am task 2"); } } };
Task2 is interrpted, however Task1 is never interrupted and continues execution inside the loop. I canβt put the logic inside the client code (something similar to for-loop). I need help from the SO community to solve this problem. Thanks.
source share