我有一个多线程应用程序,并通过setName()
属性为每个线程分配一个惟一的名称。现在,我希望功能可以使用线程的相应名称直接访问它们。
类似于下面的函数:
public Thread getThreadByName(String threadName) {
Thread __tmp = null;
Set<Thread> threadSet = Thread.getAllStackTraces().keySet();
Thread[] threadArray = threadSet.toArray(new Thread[threadSet.size()]);
for (int i = 0; i < threadArray.length; i++) {
if (threadArray[i].getName().equals(threadName))
__tmp = threadArray[i];
}
return __tmp;
}
上面的函数检查所有运行的线程,然后从运行的线程集中返回所需的线程。也许我想要的线程被中断了,那么上面的函数就不起作用了。有没有关于如何合并该功能的想法?
https://stackoverflow.com/questions/15370120
复制相似问题