「查漏补缺」线程组

119 阅读2分钟

我正在参加「掘金·启航计划」

看代码的时候,偶然发现了一个东西ThreadGroup,也很丢人,一直没有看到过这个,这里就写个笔记记录下

先看注释

/**
 * A thread group represents a set of threads. In addition, a thread
 * group can also include other thread groups. The thread groups form
 * a tree in which every thread group except the initial thread group
 * has a parent.
 一个线程组代表一堆线程,此外,一个线程组可以包括其他线程组。线程组形成了一棵树,其中除了初始线程组之外的每个线程组都有一个父级。
 * <p>
 * A thread is allowed to access information about its own thread
 * group, but not to access information about its thread group's
 * parent thread group or any other thread groups.
 *
 一个线程允许访问有关自己线程组的信息,但不能访问它的线程组的父线程组或任何其他线程组的信息
 * @author  unascribed
 * @since   JDK1.0
 */
public static void main(String[] args) throws InterruptedException {
        Object key=new Object();
        ThreadGroup parentThreadGroup=new ThreadGroup("parent");
        ThreadGroup sonThreadGroup=new ThreadGroup(parentThreadGroup,"son");

        new Thread(parentThreadGroup,()->{
            synchronized (key){
                try {
                    System.out.println("------------");

                    System.out.println("parent start.....");
                    Thread.sleep(3000);
                    System.out.println("parent over.....");
                    System.out.println(Thread.currentThread().getThreadGroup().getName());
                    System.out.println(Thread.currentThread().getThreadGroup().getParent().getName());
                    System.out.println(Thread.currentThread().getThreadGroup().getParent().getParent().getName());
                    System.out.println(Thread.currentThread().getThreadGroup().getParent().getParent().getParent().getName());

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();


        new Thread(sonThreadGroup,()->{
            synchronized (key){
                try {
                    System.out.println("------------");

                    System.out.println("son start ......");
                    Thread.sleep(3000);
                    System.out.println("son over ......");
                    System.out.println(Thread.currentThread().getThreadGroup().getName());
                    System.out.println(Thread.currentThread().getThreadGroup().getParent().getName());
                    System.out.println(Thread.currentThread().getThreadGroup().getParent().getParent().getName());
                    System.out.println(Thread.currentThread().getThreadGroup().getParent().getParent().getParent().getName());

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        new Thread(()->{
            synchronized (key){
                try {
                    System.out.println("------------");

                    System.out.println("other start ......");
                    Thread.sleep(3000);
                    System.out.println("other over ......");
                    System.out.println(Thread.currentThread().getThreadGroup().getName());
                    System.out.println(Thread.currentThread().getThreadGroup().getParent().getName());
                    System.out.println(Thread.currentThread().getThreadGroup().getParent().getParent().getName());
                    System.out.println(Thread.currentThread().getThreadGroup().getParent().getParent().getParent().getName());

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        Thread.sleep(1000);
        try{

            //销毁此线程组及其所有子组。此线程组必须为空,表示该线程组中的所有线程都已停止。
//            parentThreadGroup.destroy();
//            System.out.println("isDestory?:"+parentThreadGroup.isDestroyed());
            //停止此线程组中的所有线程。(废弃)
//            parentThreadGroup.stop();
            //中断此线程组中的所有线程。
//        parentThreadGroup.interrupt();
            System.out.println(            parentThreadGroup.activeCount());
        }catch (Exception e){
            System.out.println("isDestory?:"+parentThreadGroup.isDestroyed());

        }

        new Thread(sonThreadGroup,()->{
            synchronized (key){
                try {
                    System.out.println("------------");
                    System.out.println("later start ......");
                    Thread.sleep(3000);
                    System.out.println("later over ......");
                    System.out.println(Thread.currentThread().getThreadGroup().getName());
                    System.out.println(Thread.currentThread().getThreadGroup().getParent().getName());
                    System.out.println(Thread.currentThread().getThreadGroup().getParent().getParent().getName());
                    System.out.println(Thread.currentThread().getThreadGroup().getParent().getParent().getParent().getName());

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }

结果

------------
parent start.....
2
parent over.....
parent
main
system
------------
later start ......
Exception in thread "Thread-0" java.lang.NullPointerException
	at com.mountain.monk.netty1.ThreadGroupDemo.lambda$main$0(ThreadGroupDemo.java:21)
	at java.lang.Thread.run(Thread.java:748)
later over ......
son
parent
main
system
------------
other start ......
other over ......
main
system
------------
son start ......
Exception in thread "Thread-2" java.lang.NullPointerException
	at com.mountain.monk.netty1.ThreadGroupDemo.lambda$main$2(ThreadGroupDemo.java:59)
	at java.lang.Thread.run(Thread.java:748)
son over ......
son
parent
main
system

大致总结

总的来说没啥用,可能方便管理?

线程创建都要指定一个,没有就指定为SecurityManager的线程组,并不影响锁

  • interrupt:中断当前线程组中的线程,极其子线程
  • destroy:销毁,但是感觉没啥用