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. PySide——Python图形化界面入门教程(五)

    PySide——Python图形化界面入门教程(五) ——QListWidget 翻译自:http://pythoncentral.io/pyside-pyqt-tutorial-the-qlistw ...

  2. SQLServer 可更新订阅数据在线架构更改(增加字段)方案

    原文:SQLServer 可更新订阅数据在线架构更改(增加字段)方案 之前一直查找冲突发布和订阅数据不一致的原因,后来发现多少数据库升级引起,因为一直以来都是在发布数据库增加字段,订阅也会自动同步.在 ...

  3. 请求通道在等待 00:00:58.9616639 以后答复时超时。增加传递给请求调用的超时值,或者增加绑定上的 SendTimeout 值。分配给此操作的时间可能是更长超时的一部分。

    异常信息:请求通道在等待 00:00:58.9616639 以后答复时超时.增加传递给请求调用的超时值,或者增加绑定上的 SendTimeout 值.分配给此操作的时间可能是更长超时的一部分. 开发背 ...

  4. CROSS JOIN

    原文:CROSS JOIN 最近在讲到T-SQL查询的Join部分时,一下子没有想起来CROSS JOIN的用法,因为其实平常也确实基本不用到.特意找了一个例子,以供参考 CROSS JOIN又称为笛 ...

  5. Oracle配置OneMap中的sql数据库问题及解决方案

    报错ORA-00900:无效SQL语句,点确定后报错:ORA--00942:表或视图不存在 分析:prompt在Oracle中是打印功能,如果要在PLsql中执行带有prompt的sql文件就会报上面 ...

  6. 【MVC 笔记】MVC 自定义 Attribute 属性中的猫腻

    原想在 MVC Action 上加一个自定义 Attribute 来做一些控制操作,最先的做法是在自定 Attribute 中定义一个属性来做逻辑判断,可惜事与愿违,这个属性值居然会被缓存起来,于是于 ...

  7. 获取UWP配置文件中的版本信息

    原文:获取UWP配置文件中的版本信息 在一般的软件中,我们都会显示当前软件的版本信息.以前作者都是在发版的时候修改一下UWP的配置文件中的版本信息和软件中的版本信息.但是每次这样很麻烦,有时间忘记修改 ...

  8. QT延时方法整理(QTimer::singleShot,QWaitCondition,QDateTime.secsTo三种新方法)

    1: void QTimer::singleShot ( int msec, QObject * receiver, const char * member ) [static] 样例: #inclu ...

  9. 腾讯云直播录制遇到的bug

    1.录制方式应用:   初始化方法   [[TXUGCRecordshareInstance] startCameraCustom:param preview:_showPlayerView]; ID ...

  10. Using VNC on a debian/Ubuntu server with a OS X Mac

    I got a brand new MacBook Pro 13" 2016. I used to work on GNU/Linux for decades. I don't want t ...