日期:2014-05-20  浏览次数:20608 次

能通过线程的标识号取得某个线程吗?

long id = myThread.getId()获取了myThread线程的唯一标识,那么能通过id找回原线程吗??设线程并未结束。

------解决方案--------------------
Java code
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;

public class ThreadTest {

    public static void main(String[] args) {
        Thread t = new Thread(new InnerRunnable());
        t.start();
        System.out.println("        thread: " + t);
        long threadId = t.getId();
        
        // 一:通过线程组遍历获得线程
        Thread s = findThread(threadId);
        System.out.println("   find thread: " + s);
        System.out.println("current thread: " + Thread.currentThread());
               
        
        // 二:通过 JMX 可以通过线程 ID 获得线程信息
        ThreadMXBean tmx = ManagementFactory.getThreadMXBean();
        ThreadInfo info = tmx.getThreadInfo(threadId);
        System.out.println(info.getThreadState());
        
        
        s.interrupt();
    }

    /**
     * 通过线程组获得线程
     *
     * @param threadId
     * @return
     */
    public static Thread findThread(long threadId) {
        ThreadGroup group = Thread.currentThread().getThreadGroup();
        while(group != null) {
            Thread[] threads = new Thread[(int)(group.activeCount() * 1.2)];
            int count = group.enumerate(threads, true);
            for(int i = 0; i < count; i++) {
                if(threadId == threads[i].getId()) {
                    return threads[i];
                }
            }
            group = group.getParent();
        }
        return null;
    }

    private static class InnerRunnable implements Runnable {

        private int i = 0;

        public void run() {
            try {
                while (!Thread.currentThread().isInterrupted()) {
                    System.out.println(i++);
                    Thread.sleep(10);
                }
            } catch (InterruptedException e) {
                System.out.println("mythread is interrupted!");
            }
        }
    }
}

------解决方案--------------------
楼上的代码很好,学习了!