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. Android零基础入门第44节:ListView数据动态更新

    原文:Android零基础入门第44节:ListView数据动态更新 经过前面几期的学习,关于ListView的一些基本用法大概学的差不多了,但是你可能发现了,所有ListView里面要填充的数据都是 ...

  2. C#WebBroswer控件的使用

    在WebBroswer中可以嵌入一个网页文件,通过Url属性绑定. URI,统一资源标识符,用来唯一的标识一个资源. URL,统一资源定位器,它是一种具体的URI,即URL可以用来标识一个资源. 它包 ...

  3. Newtonsoft.Json高级用法之枚举中文转义

    最近看博客园中 焰尾迭的两篇关于"Newtonsoft.Json高级用法"的文章受到了很多人的评论,一度登入到头条推荐. 今天我就不再重复焰尾迭博文中的一些提过的Newtonsof ...

  4. 基于 libevent 开发的 C++ 11 高性能网络服务器 evpp(360的作品)

    evpp是一个基于libevent开发的现代化C++11高性能网络服务器,自带TCP/UDP/HTTP等协议的异步非阻塞式的服务器和客户端库. 特性: 现代版的C++11接口 非阻塞异步接口都是C++ ...

  5. Qt 中C++ static_cast 和 reinterpret_cast的区别(static_cast是隐式类型转换,会有数据损失,reinterpret_cast是底层二进制转换,没有数据损失)

    1.C++中的static_cast执行非多态的转换,用于代替C中通常的转换操作.因此,被做为隐式类型转换使用.比如: int i; float f = 166.7f; i = static_cast ...

  6. XPath概述

    1.  XPath 具体示例可参考网址: http://www.zvon.org/xxl/XPathTutorial/General/examples.html 1.1 概述 * 现节点下所有元素 * ...

  7. 编译Qt5.0连接MySql5.5数据库的驱动(5.0版本的编译,我记得5.2开始自带了)

    第一步 1.准备好Mysql数据库安装文件,Qt5.0完整的离线安装包,以及Qt5.0的完整的源代码.安装好程序,假设Mysql的安装路径为:C:\MySQL5.5,Qt5.0的安装路径:C:\Qt\ ...

  8. 今天想安装 windowsl ive 提示安装失败 错误码

    Windows Live installation error: OnCatalogResult: 0x80072ee6 看了了这个老兄的回答,试了试,果然OK,谢谢@普洛提亚从这里下载安装包,然后安 ...

  9. 使用 docker 搭建 nginx+php-fpm 环境 (两个独立镜像)

    :first-child{margin-top:0!important}.markdown-body>:last-child{margin-bottom:0!important}.markdow ...

  10. EditPlus 格式化HTML JS CSS

    首先你得安装一个EditPlus,然后下载你想格式化的xxxformatter.js文件,也就是网上说的EDTOOLS 1.在你的工具里边找到-->配置自定义工具 具体做法如下: 第5步 命令: ...