在Java中使用线程ID终止线程 [英] Terminating thread using thread Id in java

查看:87
本文介绍了在Java中使用线程ID终止线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用其ID终止线程.

I want to terminate thread using it's Id.

使用下面的语句,我得到线程.我在 Hashtable 中维护的该线程ID,但是每当使用它要终止该线程时,我就有可能使用线程ID终止.

using below statement I'm getting thread. this thread Id I'm maintaining in Hashtable,but whenever use want to terminate the thread it will be possible for me to termiate using thread Id.

long threadId=Thread.currentThread().getId();   

我该如何实现?

推荐答案

您可以这样做,

//Give you set of Threads
Set<Thread> setOfThread = Thread.getAllStackTraces().keySet();

//Iterate over set to find yours
for(Thread thread : setOfThread){
    if(thread.getId()==yourThread.getId()){
        thread.interrupt();
    }
}

这篇关于在Java中使用线程ID终止线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆