1

Code which I am trying

public void killJob(String thread_id)throws RemoteException{   
Thread t1 = new Thread(a);   
    t1.suspend();   
}

How can we suspend/pause thread based on its id? Thread.suspend is deprecated,There must be some alternative to achieve this. I have thread id I want to suspend and kill the thread.

Edit: I used this.

AcQueryExecutor a=new AcQueryExecutor(thread_id_id);
Thread t1 = new Thread(a); 
t1.interrupt(); 
while (t1.isInterrupted()) { 
    try { 
       Thread.sleep(1000); 
    } catch (InterruptedException e) { 
       t1.interrupt(); 
       return; 
    } 
} 

But I am not able to stop this thread.

jww
  • 97,681
  • 90
  • 411
  • 885
happy
  • 2,550
  • 17
  • 64
  • 109
  • Oh I see, you added a question. You don't put the `isInterrupted()` check in the main thread. You put it inside of the `AcQueryExecutor` code. The `AcQueryExecutor` thread should be checking to see if its own thread (itself) has been interrupted. The `while` loop in the caller should never run because `t1.interrupted()` will always be true after you just called `t1.interrupt()`. – Gray Jun 22 '12 at 14:52
  • I've edited my answer to make this more plain. Sorry for the confusion. – Gray Jun 22 '12 at 14:57

2 Answers2

8

How can we suspend/pause thread based on its id? ... I have thread id I want to suspend and kill the thread.

The right way to kill a thread these days is to interrupt() it. That sets the Thread.isInterrupted() to true and causes wait(), sleep(), and a couple other methods to throw InterruptedException.

Inside of your thread code, you should be doing something like the following which checks to make sure that it has not been interrupted.

 // run our thread while we have not been interrupted
 while (!Thread.currentThread().isInterrupted()) {
     // do your thread processing code ...
 }

Here's an example of how to handle interrupted exception inside of a thread:

 try {
     Thread.sleep(...);
 } catch (InterruptedException e) {
     // always good practice because throwing the exception clears the flag
     Thread.currentThread().interrupt();
     // most likely we should stop the thread if we are interrupted
     return;
 }

The right way to suspend a thread is a bit harder. You could set some sort of volatile boolean suspended flag for the thread that it would pay attention to. You could also use object.wait() to suspend a thread and then object.notify() to start it running again.

Gray
  • 115,027
  • 24
  • 293
  • 354
  • execution nerver enters in the above block. I have added this AcQueryExecutor a=new AcQueryExecutor(thread_id_id); Thread t1 = new Thread(a); t1.interrupt(); while (t1.isInterrupted()) { try { Thread.sleep(1000); } catch (InterruptedException e) { t1.interrupt(); return; } } – happy Jun 14 '12 at 04:33
  • I don't quite understand @happy. Have you started the thread? That code will work if the thread has been started and has been interrupted. – Gray Jun 14 '12 at 12:11
  • @happy not(!) operator is missing in the while loop and corrected now. – Kanagavelu Sugumar May 12 '16 at 10:57
  • Thanks! Gray, it is an great answer! – Kanagavelu Sugumar May 12 '16 at 11:05
  • Thanks a ton. Was looking for this kind of functionality. One question, is there any memory leak since that thread is actually not killed. Anything else that i could do to cleanly kill that thread ?? – Raghav May 02 '17 at 15:58
  • If the thread returns and exits then it can be reaped. If it is still running then it won't be @Raghav. – Gray May 02 '17 at 20:34
0

I posted a PauseableThread implementation recently that uses a ReadWriteLock internally. Use one of these or a variant and you should be able to pause your threads.

As to pausing them by id, a little googling suggests a way to iterate over all threads which looks like it should work. Thread has exposed a getId method for some time.

Killing the thread is different. @Gray has neatly covered that one.

Community
  • 1
  • 1
OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213