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. GIS基础软件及操作(一)

    原文  GIS基础软件及操作(一) 练习一.浏览地理数据 使用 ArcGIS浏览地理数据 第1步 启动 ArcMap 启动ArcMap.执行菜单命令:开始>>所有程序>> Ar ...

  2. Ext5.1日期控件仅显示年月

    1.注册xtype类型 2.保存文件为xxxx.js 3.使用 xtype : monthfield return this.buildToolbar({ items: [ { xtype: 'mon ...

  3. EF 里的 join and Group Join

    join ); pageCount = _db.Orders.Count(); return _db.Orders.OrderByDescending(c=>c.ID).Skip(skip).T ...

  4. QML Settings 小的示例

    QML 中使用 Settings 可以保存一些简单的信息,例如用户名,密码,窗口位置,大小等,没有Sqlite那么麻烦,简单易用哦~~~(环境:Qt5.8  for android ,Windows ...

  5. 转换GMT秒数为日期时间格式-Delphi源码

    转换GMT秒数为日期时间格式-Delphi源码.收藏最近在写PE分析工具的时候,需要转换TimeDateStamp字段值为日期时间格式,这是Delphi的源码. //把GMT时间的秒数转换成日期时间格 ...

  6. Lucene Index Search

    转发自:  https://my.oschina.net/u/3777556/blog/1647031 什么是Lucene?? Lucene 是 apache 软件基金会发布的一个开放源代码的全文检索 ...

  7. 《解读window核心编程》 之 进程

    1.         进程是执行文件的运行时形态.包括两部分:内核数据(对应内核对象).地址空间(包括执行文件代码和栈堆等动态内存). 2.         把VC的“系统-子系统”值删除掉,即不指定 ...

  8. C#中比较两个对象的地址是否相同(也是引用计数的问题,和Java一样)

    private void button1_Click(object sender, EventArgs e) {     char[] ch = { 'z', 's', 'w', 'a', 'n',  ...

  9. php生成html静态文件

    现在的动态网站存在很多性能上的弊端,seo优化会存在一定的瓶颈,现在将动态的网站代码转换为html静态文件,是浏览器通过html间接的读取动态网站源文件,这对其网站加载速度还是seo优化有着举足轻重的 ...

  10. 错误处理之try、catch、finally中的return、throw执行顺序。

    今天遇到一个让人无语的代码块 try { bilSheetService.syncUser(bilWebseviceLog, userId, optType); }catch (Exception e ...