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());                              
    }                                                                          
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值