我想用它的Id终止线程.
使用下面的语句我得到线程.这个线程我正在维护Hashtable,但每当使用想要终止线程时,我就可以使用线程ID来终止.
long threadId=Thread.currentThread().getId();
Run Code Online (Sandbox Code Playgroud)
我怎样才能实现它?
Cod*_*roc 10
你可以这样做,
//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();
}
}
Run Code Online (Sandbox Code Playgroud)