java根据线程的id去获取线程
来源:互联网 发布:知夫莫若妻 编辑:程序博客网 时间:2024/04/29 19:42
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!"); } } }}
0 0
- java根据线程的id去获取线程
- 根据线程ID获取其Handle
- 获取当前进程ID和子线程的线程ID
- Linux获取线程ID
- 获取线程ID
- 获取线程ID
- 获取线程ID
- Liunx获取线程Id
- 3.4 线程id获取
- linux获取线程ID
- linux 获取线程ID的方法
- linux 获取线程ID的方法
- 获取当前线程的id号 gettid()
- 获取线程id的另一种方法
- Linux下获取线程ID的方法
- Linux获取线程id的方法学习
- 根据线程ID找出其所在的模块名
- VC 根据线程句柄获得线程ID 根据线程句柄获得进程ID
- jqmobi(Appframework)和iscroll4兼容的问题解决
- 2014-07-16 13:24
- 2014-07-16 15:22
- 2014-07-16 15:22
- 关于TextView的setText()与Integer之间一个易犯的小错误
- java根据线程的id去获取线程
- 2014-07-16 15:54
- 2014-07-17 16:44
- 2014-07-17 17:04
- 面
- 2014-07-18 10:25
- STM32 定时器级联库说明文档
- 2014-07-18 11:01
- 07分布式数据仓库 HIVE -- 函数