找它出来,然后
Thread.setName(ThreadName)
public Thread findThread(String target)
{
int SAFETY_MARGIN = 10;
// Find master ThreadGroup which all others descend
ThreadGroup rootGroup = Thread.currentThread().getThreadGroup();
while(rootGroup.getParent() != null)
{
rootGroup = rootGroup.getParent();
}
Thread threadList[] = new Thread[rootGroup.activeGroupCount() + SAFETY_MARGIN];
int count = rootGroup.enumerate(threadList);
Thread aThread;
for(int i = 0; i < count; i ++ )
{
aThread = threadList[i];
if(aThread == null)
continue;
if(aThread.getName().equals(target))
{
return aThread;
}
}
return null;
}
---------------
[email protected]
Thread.setName(ThreadName)
public Thread findThread(String target)
{
int SAFETY_MARGIN = 10;
// Find master ThreadGroup which all others descend
ThreadGroup rootGroup = Thread.currentThread().getThreadGroup();
while(rootGroup.getParent() != null)
{
rootGroup = rootGroup.getParent();
}
Thread threadList[] = new Thread[rootGroup.activeGroupCount() + SAFETY_MARGIN];
int count = rootGroup.enumerate(threadList);
Thread aThread;
for(int i = 0; i < count; i ++ )
{
aThread = threadList[i];
if(aThread == null)
continue;
if(aThread.getName().equals(target))
{
return aThread;
}
}
return null;
}
---------------
[email protected]
杀死当前Thread!不过建议不要使用!
建议用core java作者所说,定义一个变量来表示这个线程是否有终止的要,让线程定期检测这个变量。
比如:
public class MyThread extends Thread {
public void run() {
while(!stopRequested && more work to do) {
do more work
}
}
public void requestStop() {
stopRequested = true;
}
private boolean stopRequested;
}
while(live){
//do your work
}
}when you want to kill your thread safely
turn the live = false