简体   繁体   English

如何使用线程的 id 挂起线程?

[英]How to suspend thread using thread's id?

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?我们如何根据其 id 挂起/暂停线程? Thread.suspend is deprecated,There must be some alternative to achieve this. Thread.suspend 已被弃用,必须有一些替代方案来实现这一点。 I have thread id I want to suspend and kill the thread.我有线程 id 我想挂起并终止线程。

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.但我无法停止这个线程。

How can we suspend/pause thread based on its id?我们如何根据其 id 挂起/暂停线程? ... I have thread id I want to suspend and kill the thread. ...我有线程 id 我想挂起并终止线程。

The right way to kill a thread these days is to interrupt() it.如今杀死线程的正确方法是interrupt()它。 That sets the Thread.isInterrupted() to true and causes wait() , sleep() , and a couple other methods to throw InterruptedException .这将Thread.isInterrupted()为 true 并导致wait()sleep()和其他几个方法抛出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.您可以为要关注的线程设置某种volatile boolean suspended标志。 You could also use object.wait() to suspend a thread and then object.notify() to start it running again.您还可以使用object.wait()挂起线程,然后使用object.notify()再次启动它运行。

I posted a PauseableThread implementation recently that uses a ReadWriteLock internally.我最近发布了一个在内部使用ReadWriteLockPauseableThread实现。 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.至于通过 id 暂停它们,一点谷歌搜索建议一种方法来迭代所有看起来应该工作的线程 Thread has exposed a getId method for some time. Thread公开了一个getId方法有一段时间了。

Killing the thread is different.杀死线程是不同的。 @Gray has neatly covered that one. @Gray巧妙地涵盖了那个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM