Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to interrupt the Thread when it is inside some loop doing long task?
    primarykey
    data
    text
    <p><strong>Possible Duplicate:</strong> <a href="https://stackoverflow.com/questions/671049/how-do-you-kill-a-thread-in-java">How do you kill a thread in Java?</a></p> <hr> <p>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. </p> <p>Task are something similar to:</p> <pre><code>public class Task1 extends Thread { public void run() { while(true){ if(Thread.interrupted()){ return; } for(int i=0; i&lt;Integer.MAX_VALUE; i++){ System.out.println("I am task 1 " + i); } } } }; </code></pre> <p>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.</p> <pre><code>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&lt;?&gt; runningTask1 = executorService.submit(task1); Task2 task2 = new Task2(); Future&lt;?&gt; 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()); } </code></pre> <p>}</p> <p>Task2 is :</p> <pre><code>public class Task2 extends Thread{ public void run() { while(true){ if(Thread.interrupted()){ return; } System.out.println("I am task 2"); } } }; </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload