一、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();