ThreadGroup解讀

ThreadGroup解讀

ThreadGroup 可以把thread的名字統一起來。一起處理catch。

ThreadGroup是Java提供的一種對線程進行分組管理的手段,可以對所有線程以組爲單位進行操作,如設置優先級、守護線程等。

線程組也有父子的概念,如下圖:
在這裏插入圖片描述

線程組的基本操作

1.1 線程組的創建

public class ThreadGroupCreator {                                              
                                                                               
    public static void main(String[] args) {                                   
        //獲取當前線程的group                                                         
        ThreadGroup currentGroup = Thread.currentThread().getThreadGroup();    
        //在當前線程執行流中新建一個Group1                                                  
        ThreadGroup group1 = new ThreadGroup("Group1");                        
        //Group1的父線程,就是main線程所在Group                                           
        System.out.println(group1.getParent() == currentGroup);                
        //定義Group2, 指定group1爲其父線程                                              
        ThreadGroup group2 = new ThreadGroup(group1, "Group2");                
        System.out.println(group2.getParent() == group1);                      
    }                                                                          
}

ThreadGroup是位於java.lang包下的一個類,用於統一的線程管理.一旦一個線程加入到一個線程組後,就不能更換線程所在的線程組

1.2 複製線程組中活躍的線程信息

Thread[] threads = new Thread[num];
threadGroup.enumerate(threads);
for (Thread t : threads) {
	System.out.println("線程名-" + t.getName());
}

1.3 線程組的基本操作

public class ThreadGroupBasic {                                                      
                                                                                     
    public static void main(String[] args) throws InterruptedException {             
                                                                                     
        ThreadGroup group = new ThreadGroup("group1");                               
        Thread thread = new Thread(group, () -> {                                    
            while(true) {                                                            
                try {                                                                
                    TimeUnit.SECONDS.sleep(1);                                       
                } catch (InterruptedException e) {                                   
                    e.printStackTrace();                                             
                }                                                                    
            }                                                                        
        }, "thread");                                                                
        thread.setDaemon(true);                                                      
        thread.start();                                                              
                                                                                     
        TimeUnit.MILLISECONDS.sleep(1);                                              
                                                                                     
        ThreadGroup mainGroup = Thread.currentThread().getThreadGroup();             
        //遞歸獲取mainGroup中活躍線程的估計值                                                     
        System.out.println("activeCount = " + mainGroup.activeCount());              
        //遞歸獲mainGroup中的活躍子group                                                     
        System.out.println("activeGroupCount = " + mainGroup.activeGroupCount());    
        //獲取group的優先級, 默認爲10                                                         
        System.out.println("getMaxPriority = " + mainGroup.getMaxPriority());        
        //獲取group的名字                                                                 
        System.out.println("getName = " + mainGroup.getName());                      
        //獲取group的父group, 如不存在則返回null                                                
        System.out.println("getParent = " + mainGroup.getParent());                  
        //活躍線程信息全部輸出到控制檯                                                             
        mainGroup.list();                                                            
        System.out.println("----------------------------");                          
        //判斷當前group是不是給定group的父線程, 如果兩者一樣,也會返回true                                   
        System.out.println("parentOf = " + mainGroup.parentOf(group));               
        System.out.println("parentOf = " + mainGroup.parentOf(mainGroup));           
                                                                                     
    }                                                                                
                                                                                     
}

線程組的其他操作

2.1 線程組的Interrupt

public class ThreadGroupInterrupt {                                                     
                                                                                       
    public static void main(String[] args) throws InterruptedException {               
        ThreadGroup group = new ThreadGroup("TestGroup");                              
        new Thread(group, () -> {                                                      
            while(true) {                                                              
                try {                                                                  
                    TimeUnit.MILLISECONDS.sleep(2);                                    
                } catch (InterruptedException e) {                                     
                    //received interrupt signal and clear quickly                      
                    System.out.println(Thread.currentThread().isInterrupted());        
                    break;                                                             
                }                                                                      
            }                                                                          
            System.out.println("t1 will exit");                                        
        }, "t1").start();                                                              
        new Thread(group, () -> {                                                      
            while(true) {                                                              
                try {                                                                  
                    TimeUnit.MILLISECONDS.sleep(2);                                    
                } catch (InterruptedException e) {                                     
                    //received interrupt signal and clear quickly                      
                    System.out.println(Thread.currentThread().isInterrupted());        
                    break;                                                             
                }                                                                      
            }                                                                          
            System.out.println("t2 will exit");                                        
        }, "t2").start();                                                              
        //make sure all threads start                                                  
        TimeUnit.MILLISECONDS.sleep(2);                                                
                                                                                       
        group.interrupt();                                                             
    }                                                                                  
                                                                                       
}

2.2 線程組的destroy

public class ThreadGroupDestroy {                                             
                                                                              
    public static void main(String[] args) {                                  
        ThreadGroup group = new ThreadGroup("TestGroup");                     
        ThreadGroup mainGroup = Thread.currentThread().getThreadGroup();      
        //before destroy                                                      
        System.out.println("group.isDestroyed=" + group.isDestroyed());       
        mainGroup.list();                                                     
                                                                              
        group.destroy();                                                      
        //after destroy                                                       
        System.out.println("group.isDestroyed=" + group.isDestroyed());       
        mainGroup.list();                                                     
    }                                                                         
                                                                              
}

2.3 線程組設置守護線程組

線程組設置爲守護線程組,並不會影響其線程是否爲守護線程,僅僅表示當它內部沒有active的線程的時候,會自動destroy。

public class ThreadGroupDaemon {                                               
                                                                               
    public static void main(String[] args) throws InterruptedException {       
        ThreadGroup group1 = new ThreadGroup("group1");                        
        new Thread(group1, () -> {                                             
            try {                                                              
                TimeUnit.SECONDS.sleep(1);                                     
            } catch (InterruptedException e) {                                 
                e.printStackTrace();                                           
            }                                                                  
        }, "group1-thread1").start();                                          
        ThreadGroup group2 = new ThreadGroup("group2");                        
        new Thread(group2, () -> {                                             
            try {                                                              
                TimeUnit.SECONDS.sleep(1);                                     
            } catch (InterruptedException e) {                                 
                e.printStackTrace();                                           
            }                                                                  
        }, "group1-thread2").start();                                          
        group2.setDaemon(true);                                                
                                                                               
        TimeUnit.SECONDS.sleep(3);                                             
        System.out.println(group1.isDestroyed());                              
        System.out.println(group2.isDestroyed());                              
    }                                                                          
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章