我想使用其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)!