import java.util.concurrent.TimeUnit;

public class Test {
    public static void main(String[] args){
//        ThreadGroupCreater.test();
//        ThreadGroupEnumerateThreads.test();
//        ThreadGroupEnumerateThreadGroups.test();
//        ThreadGroupBasic.test();
//        ThreadGroupInterrupt.test();
//        ThreadGroupDestroy.test();
        ThreadGroupDaemon.test();
    }

}

/*
    6.2 创建ThreadGroup

        public ThreadGroup(String name)
        public ThreadGroup(ThreadGroup parent, String name)
 */
class ThreadGroupCreater{
    public static void test() {
        ThreadGroup currentGroup = Thread.currentThread().getThreadGroup();

        ThreadGroup group1 = new ThreadGroup("Group1");
        ThreadGroup group2 = new ThreadGroup(group1,"Group2");

        System.out.println(group1.getParent()==currentGroup);
        System.out.println(group2.getParent()==group1);
    }

    /*
        6.3.1 复制Thread数组

            public int enumerate(Thread[] list)                 :将ThreadGroup中的所有active线程复制到list中
            public int enumerate(Thread[] list,boolean recurse) :将ThreadGroup中的所有active线程复制到list中
                如果recurse为true,则递归的将所有子group中的线程也复制到list中

            enumerate(Thread[] list)等价与enumerate(Thread[] list, true)

        enumerate方法的返回值int相较Thread[]的长度更为真实,因为可能受数组长度的限制
        导致部分活跃线程数量没有放入数组中。
     */
}
class ThreadGroupEnumerateThreads{
    public static void test() {
        ThreadGroup myGroup = new ThreadGroup("MyGroup");

        Thread thread = new Thread(myGroup,()->{
            while (true) {
                try{
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"MyThread");
        thread.start();

        try {
            TimeUnit.SECONDS.sleep(2);
            ThreadGroup mainGroup = Thread.currentThread().getThreadGroup();

            /*
                用法展示在这里:
                    1.先定义一个数组,通过activeCount方法得到这个数组的长度
                    2.将这个数组传入到enumerate方法中
                    3.展示结果。
             */
            Thread[] list = new Thread[mainGroup.activeCount()];
            int recurseSize = mainGroup.enumerate(list);
            System.out.println(recurseSize);

            recurseSize = mainGroup.enumerate(list,true);
            System.out.println(recurseSize);

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
/*
    6.3.2 复制ThreadGroup数组

        public int enumerate(ThreadGroup[] list)
        public int enumerate(ThreadGroup[] list, boolean recurse)
 */
class ThreadGroupEnumerateThreadGroups{
    public static void test() {
        ThreadGroup myGroup1 = new ThreadGroup("MyGroup1");
        ThreadGroup myGroup2 = new ThreadGroup(myGroup1,"MyGroup2");
        ThreadGroup mainGroup = Thread.currentThread().getThreadGroup();

        try {
            TimeUnit.MILLISECONDS.sleep(2);

            ThreadGroup[] list = new ThreadGroup[mainGroup.activeCount()];
            int recurseSize = mainGroup.enumerate(list);
            System.out.println(recurseSize);

            recurseSize = mainGroup.enumerate(list,false);
            System.out.println(recurseSize);

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

/*
    6.4.1 ThreadGroup的基本操作

        activeCount()      :用于获取group中活跃的线程
        activeGroupCount() :用于获取group中活跃的子group

        getName()          :用于获取group的名字
        getParent()        :用于过于group父group的名字

        list()             :将group中所有活跃线程信息输出到控制台
        parentOf()         :判断当前group是不是给定group的父group

        getMaxPriority():
        setMaxPriority():

    setMaxPriority()只会限制之后加入的线程最大优先级,不会修改加入之前的线程
    parentOf()对自生调用这个方法,返回的结果是true
 */
class ThreadGroupBasic {
    public static void test(){
        ThreadGroup group = new ThreadGroup("group");
        ThreadGroup mainGroup = Thread.currentThread().getThreadGroup();

        Thread thread = new Thread(group,()->{
            while (true) {
                try{
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"thread");
        thread.setDaemon(true);
        thread.start();

        try {
            TimeUnit.MILLISECONDS.sleep(1);

            System.out.println("     activeCount="+mainGroup.activeCount());
            System.out.println("activeGroupCount="+mainGroup.activeGroupCount());
            System.out.println("  getMaxPriority="+mainGroup.getMaxPriority());
            System.out.println("         getName="+mainGroup.getName());
            System.out.println("       getParent="+mainGroup.getParent());

            mainGroup.list();

            System.out.println("================================");
            System.out.println("parentOf?"+mainGroup.parentOf(group));
            System.out.println("parentOf?"+mainGroup.parentOf(mainGroup));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

/*
    6.4.2 ThreadGroup的interrupt

    interrupt一个thread group会呆滞该group中所有的active线程都被interrupt。

*/
class ThreadGroupInterrupt{
    public static void test(){
        ThreadGroup group = new ThreadGroup("TestGroup");
        new Thread(group,()->{
            while (true) {
                try{
                    TimeUnit.MILLISECONDS.sleep(2);
                } catch (InterruptedException e) {
                    break;
                }
            }
            System.out.println("t1 will exit...");
        },"t1").start();
        new Thread(group,()->{
            while (true) {
                try{
                    TimeUnit.MILLISECONDS.sleep(2);
                } catch (InterruptedException e) {
                    break;
                }
            }
            System.out.println("t2 will exit...");
        },"t2").start();

        try {
            TimeUnit.MILLISECONDS.sleep(3);
            group.interrupt();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
/*
    6.4.3 ThreadGroup的destroy

    destroy用于销毁ThreadGroup,该方法只是针对一个没有任何active线程的group进行一次
    destroy标记,调用该方法的直接结果是在父group中将自己移除。如果有active线程存在,则
    会抛出一个错误
 */
class ThreadGroupDestroy{
    public static void test() {
        ThreadGroup group = Thread.currentThread().getThreadGroup();
        System.out.println("group.isDestroyed=" + group.isDestroyed());
        group.list();
        group.destroy();
        System.out.println("group.isDestroyed=" + group.isDestroyed());
        group.list();
    }
}
/*
    6.4.4 守护ThreadGroup

    如果一个ThreadGroup的daemon被设置为true,那么在group中没有任何active线程的时候
    该group将自动destroy。
 */
class ThreadGroupDaemon {
    public static void test() {
        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("Group1");
        new Thread(group2,()->{
            try{
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"group2-thread1").start();

        group2.setDaemon(true);

        try {
            TimeUnit.SECONDS.sleep(3);
            System.out.println(group1.isDestroyed());
            System.out.println(group2.isDestroyed());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

——《Java高并发编程详解》笔记

ThreadGroup详细讲解的更多相关文章

  1. head标签详细讲解

    head标签详细讲解 head位于html网页的头部,后前的标签,并以开始以结束的一html标签. Head标签位置如图: head标签示意图 head包含标签 meta,title,link,bas ...

  2. 详细讲解nodejs中使用socket的私聊的方式

    详细讲解nodejs中使用socket的私聊的方式 在上一次我使用nodejs+express+socketio+mysql搭建聊天室,这基本上就是从socket.io的官网上的一份教程式复制学习,然 ...

  3. iOS KVC详细讲解

    iOS KVC详细讲解 什么是KVC? KVC即NSKeyValueCoding,就是键-值编码的意思.一个非正式的 Protocol,是一种间接访问对象的属性使用字符串来标识属性,而不是通过调用存取 ...

  4. Android webservice的用法详细讲解

    Android webservice的用法详细讲解 看到有很多朋友对WebService还不是很了解,在此就详细的讲讲WebService,争取说得明白吧.此文章采用的项目是我毕业设计的webserv ...

  5. 详细讲解Android对自己的应用代码进行混淆加密防止反编译

    1.查看项目中有没有proguard.cfg. 2.如果没有那就看看这个文件中写的什么吧,看完后将他复制到你的项目中. -optimizationpasses 5 -dontusemixedcasec ...

  6. 详细讲解Hadoop源码阅读工程(以hadoop-2.6.0-src.tar.gz和hadoop-2.6.0-cdh5.4.5-src.tar.gz为代表)

    首先,说的是,本人到现在为止,已经玩过.                   对于,这样的软件,博友,可以去看我博客的相关博文.在此,不一一赘述! Eclipse *版本 Eclipse *下载 Jd ...

  7. [iOS]数据库第三方框架FMDB详细讲解

    [iOS]数据库第三方框架FMDB详细讲解 初识FMDB iOS中原生的SQLite API在进行数据存储的时候,需要使用C语言中的函数,操作比较麻烦.于是,就出现了一系列将SQLite API进行封 ...

  8. jquery插件分类与编写详细讲解

    jquery插件分类与编写详细讲解 1. 插件种类 插件其实就是对现有的方法(或者叫函数)做一个封装,方便重用提高开发效率.   jQeury主要有2种类型   1)实例对象方法插件 开发能让所有的j ...

  9. [VC++]用CTime类得到当前日期、时间、星期,格式化(详细讲解)

    用CTime类得到当前日期.时间.星期,格式化(详细讲解)2009/05/12 09:48 A.M.① 定义一个CTime类对象 CTime time; ② 得到当前时间 time = CTime:: ...

随机推荐

  1. WPF应用程序如何重启当前的Application

    // Restart current process Method 1 System.Windows.Forms.Application.Restart(); Application.Current. ...

  2. js判断图片是否存在

    var imageData = Array(); for(var i = 0; i < imageTemp.length; i++){ ajaxSizeRequest = $.ajax({ ty ...

  3. 在Delphi中创建线程,请一定使用BeginThread()代替CreateThread()创建线程!(更好的管理异常)

    在Delphi中创建线程,请一定使用BeginThread()代替CreateThread()创建线程! 如果直接使用Win32的API函数CreateThread()创建多个线程,也是可以创建的.但 ...

  4. Angucomplete —— AngularJS 自动完成输入框

    分享 <关于我> 分享  [中文纪录片]互联网时代                 http://pan.baidu.com/s/1qWkJfcS 分享 <HTML开发MacOSAp ...

  5. Qt 5 最小构建笔记(只编译QtBase)

    只想用Qt5最基本的功能,因此只编译QtBase.也不想为了编译一个Qt装很多东西(比如非常肥的DirectX SDK) 软件清单: Visual Studio 2010 Professional w ...

  6. hive -e和hive -f的区别(转)

    大家都知道,hive -f 后面指定的是一个文件,然后文件里面直接写sql,就可以运行hive的sql,hive -e 后面是直接用双引号拼接hivesql,然后就可以执行命令. 但是,有这么一个东西 ...

  7. JAVA8之lambda表达式详解

    原文:http://blog.csdn.net/jinzhencs/article/details/50748202 lambda表达式详解 一.问题 1.什么是lambda表达式? 2.lambda ...

  8. 04 body中的相关标签

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. Python的魔术方法详解

    构造和初始化 __init__我们很熟悉了,它在对象初始化的时候调用,我们一般将它理解为"构造函数". 实际上, 当我们调用x = SomeClass()的时候调用,__init_ ...

  10. 【设计模式】行为型05责任链模式(Chain of responsibility Pattern)

    学习地址:http://www.runoob.com/design-pattern/chain-of-responsibility-pattern.html demo采用了DEBUG级别举例子,理解起 ...