Java – How to interrupt the Thread when it is inside some loop doing long task

javamultithreading

Possible Duplicate: How do you kill a thread in Java?


I need to stop doing some big task by sending the interruption signal to the Thread. I am using most of the APIs from java.util.concurrent.*. My task is send to the Thread and is executed. This task comes from the client so I do not have any control over that code.

Task are something 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 looping through the for-loop when it receives the interruption signal (Please note that I cannot put the Thread.interrputed() logic inside for-loop as it comes from the client.) I have another class that use the Executor to execute this task.

public class ConcurrentTest {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    ConcurrentTest test = new ConcurrentTest();
    ExecutorService executorService = Executors.newFixedThreadPool(2);

    Task1 task1 = new Task1();
    Future<?> runningTask1 = executorService.submit(task1);

    Task2 task2 = new Task2();
    Future<?> runningTask2 = executorService.submit(task2);

    //Stop First task after 5 second
    try {
        TimeUnit.SECONDS.sleep(5);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    runningTask1.cancel(Boolean.TRUE);
    System.out.println("runningTask1.isDone is " + runningTask1.isDone());
    System.out.println("runningTask1.isCancelled is " + runningTask1.isCancelled());
    //Stop Second task after 10 second
    try{
        TimeUnit.SECONDS.sleep(3);
    }catch(InterruptedException e){
        e.printStackTrace();
    }
    runningTask2.cancel(Boolean.TRUE);
    System.out.println("runningTask2.isDone is " + runningTask2.isDone());
    System.out.println("runningTask2.isCancelled is " + runningTask2.isCancelled());
}

}

Task2 is :

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 continue executing inside for loop. I cannot put any logic inside client code (something similar to for-loop). I need help from SO community to resolve this issue. Thanks.

Best Answer

The only bomb-proof solution is to run the client supplied code in an Isolate. An isolate is essentially a private virtual machine that a Java app can create, managed and communicate with. In particular, the parent app can safely kill an isolate and all its threads.

Reference: JSR-000121 Application Isolation API Specification - Final Release

The problem is finding a JVM that supports Isolates.