通过线程名字得到线程对象

想通过线程的名字得到线程的对象,搜索的好多发现都是得到线程对象后得到线程的名字和修改线程的名字,然并卵没有毛用。后来发现一个JDK中的好方法:

    public static Thread getThreadByName(String threadName) {
        for (Thread t : Thread.getAllStackTraces().keySet()) {
            if (t.getName().equals(threadName)) {
                System.out.println(t.getName());
                return t;
            }
        }
        return null;
    }

threadName 是你要找的线程的名字。完美搞定。。。

 

你可能感兴趣的:(通过线程名字得到线程对象)