Java如何获取线程的线程组?
使用class的getThreadGroup()方法Thread获取线程所属的线程组。
package org.nhooo.example.lang;
public class GetThreadGroup {
public static void main(String[] args) {
//创建线程组
ThreadGroup group = new ThreadGroup("ThreadGroup");
ThreadGroup anotherGroup = new ThreadGroup(group, "AnotherGroup");
//创建线程并放入线程组
Thread t1 = new Thread(group, new FirstThread(), "Thread1");
Thread t2 = new Thread(anotherGroup, new FirstThread(), "Thread2");
//启动线程
t1.start();
t2.start();
//使用Thread类的getThreadGroup()方法获取对象
//ThreadGroup然后使用getName()方法获取名称
//线程组。
System.out.format("%s is a member of %s%n", t1.getName(),
t1.getThreadGroup().getName());
System.out.format("%s is a member of %s%n", t2.getName(),
t2.getThreadGroup().getName());
}
}
class FirstThread implements Runnable {
public void run() {
System.out.println("Start..");
}
}
热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短