一、ThreadGroup概述
每个线程隶属于唯一一个线程组,这个线程组在线程创建时指定并在线程的整个生命周期内都不能更改。默认线程创建时属于main线程组。默认创建的线程组也属于main线程组,所以线程组组成了一棵以系统线程组为根的树
补充:main线程组属于system线程组,system线程组时根线程组
二、ThreadGroup常用API
三、API例子
//属于main线程组
ThreadGroup fatherGroup = new ThreadGroup("father");
//属于father线程组
ThreadGroup childGroup = new ThreadGroup(fatherGroup,"child");
//属于father线程组
Thread thread = new Thread(fatherGroup, () -> {
while (true) {
println(0);
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
thread.start();