编程技术网

关注微信公众号,定时推送前沿、专业、深度的编程技术资料。

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

zwif 线程 2022-5-13 10:41 72人围观

腾讯云服务器
在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 thread 

我该如何实现?

问题解答

您可以这样做,

//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终止线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程技术网(www.editcode.net)!

腾讯云服务器 阿里云服务器
关注微信
^