Java多線程學習(四)ThreadGroup(線程組)

一、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();
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章