import java.lang.reflect.Field; /** * description: * * @author: dawn.he QQ: 905845006 * @email: dawn.he@cloudwise.com * @email: 905845006@qq.com * @date: 2019/12/4 11:19 PM */ public class aa { public static void main(String[] args) { ThreadGroup currentGroup = Thread.currentThread().getThreadGroup(); System.out.println("main線程:" + Thread.currentThread().getId() + ":"); Field threadsField = null; Field nthreadsField = null; nthreadsField = getField(currentGroup, "nthreads"); threadsField = getField(currentGroup, "threads"); try { Object nthread = nthreadsField.get(currentGroup); int a = Integer.parseInt(nthread.toString()); Object[] o = (Object[]) threadsField.get(currentGroup); System.out.println(o.length); Thread[] thread = (Thread[]) o; if (a == 1) { System.out.println("反射main線程:" + thread[a - 1].getId() + "------------"); System.out.println("反射main線程父線程:null-------"); } else { System.out.println("反射當前線程:" + thread[a - 1].getId() + "------------"); System.out.println("反射當前父線程:" + thread[a - 2].getId() + "------------"); } } catch (IllegalAccessException e) { e.printStackTrace(); } //------------------------- new Thread(new Runnable() { public void run() { ThreadGroup currentGroup = Thread.currentThread().getThreadGroup(); System.out.println("線程1:" + Thread.currentThread().getId() + ":"); Field threadsField = null; Field nthreadsField = null; nthreadsField = getField(currentGroup, "nthreads"); threadsField = getField(currentGroup, "threads"); try { Object nthread = nthreadsField.get(currentGroup); int a = Integer.parseInt(nthread.toString()); Object[] o = (Object[]) threadsField.get(currentGroup); System.out.println(o.length); Thread[] thread = (Thread[]) o; if (a == 1) { System.out.println("反射main線程:" + thread[a - 1].getId() + "------------"); System.out.println("反射main線程父線程:null-------"); } else { System.out.println("反射當前線程1:" + thread[a - 1].getId() + "------------"); System.out.println("反射當前父線程1:" + thread[a - 2].getId() + "------------"); } } catch (IllegalAccessException e) { e.printStackTrace(); } new Thread(new Runnable() { public void run() { ThreadGroup currentGroup = Thread.currentThread().getThreadGroup(); System.out.println("線程2:" + Thread.currentThread().getId() + ":"); Field threadsField = null; Field nthreadsField = null; nthreadsField = getField(currentGroup, "nthreads"); threadsField = getField(currentGroup, "threads"); try { Object nthread = nthreadsField.get(currentGroup); int a = Integer.parseInt(nthread.toString()); Object[] o = (Object[]) threadsField.get(currentGroup); System.out.println(o.length); Thread[] thread = (Thread[]) o; if (a == 1) { System.out.println("反射main線程:" + thread[a - 1].getId() + "------------"); System.out.println("反射main線程父線程:null-------"); } else { System.out.println("反射當前線程2:" + thread[a - 1].getId() + "------------"); System.out.println("反射當前父線程2:" + thread[a - 2].getId() + "------------"); } } catch (IllegalAccessException e) { e.printStackTrace(); } new Thread(new Runnable() { public void run() { ThreadGroup currentGroup = Thread.currentThread().getThreadGroup(); System.out.println("線程3:" + Thread.currentThread().getId() + ":"); Field threadsField = null; Field nthreadsField = null; nthreadsField = getField(currentGroup, "nthreads"); threadsField = getField(currentGroup, "threads"); try { Object nthread = nthreadsField.get(currentGroup); int a = Integer.parseInt(nthread.toString()); Object[] o = (Object[]) threadsField.get(currentGroup); System.out.println(o.length); Thread[] thread = (Thread[]) o; if (a == 1) { System.out.println("反射main線程:" + thread[a - 1].getId() + "------------"); System.out.println("反射main線程父線程:null-------"); } else { System.out.println("反射當前線程3:" + thread[a - 1].getId() + "------------"); System.out.println("反射當前父線程3:" + thread[a - 2].getId() + "------------"); } } catch (IllegalAccessException e) { e.printStackTrace(); } while (true) { } } }).start(); while (true) { } } }).start(); while (true) { } } }).start(); while (true) { } } public static Field getField(ThreadGroup group, String field) { try { Field field1; field1 = group.getClass().getDeclaredField(field); if (!field1.isAccessible()) { field1.setAccessible(true); } return field1; } catch (NoSuchFieldException e) { e.printStackTrace(); } return null; } }