Java ThreadGroup

ThreadGroup的作用是对加入该组的线程进行统一的管理。《Effective Java》中认为由于ThreadGroup的很多方法后期都没有维护,所以不推荐使用,建议使用线程池来代替。

主要方法如下:

  • 构造方法

    • public ThreadGroup(String name)
      创建一个名为name的线程组

    • public ThreadGroup(ThreadGroup parent,String name)
      创建一个名为name的线程组,并指定它的父线程组为parent

  • 普通方法

    • public int activeCount();
      获得当前线程组中活跃的(还在执行的)线程数目

    • public int activeGroupCount()
      获得当前线程组中活动的子线程组的数目

    • public void checkAccess() throws SecurityException
      判断当前线程是否有权限去修改该线程组。如果当前线程不允许访问该线程组就会抛出SecurityException。

    • public int enumerate(Thread list[])
      枚举当前线程组中的线程,并将它们赋予list的每一个元素,返回线程的实际个数

    • public int enumerate(ThreadGroup list[])
      枚举当前线程组中的线程组,并将它们赋予list的每一个元素,返回线程组的实际个数

    • public final int getMaxPriority()
      获得当前线程组内优先级最高的线程的优先级

    • public final String getName()
      获得当前线程组的名字

    • public final ThreadGroup getParent()
      获得当前线程组的父线程组

    • public boolean parentOf(ThreadGroup g)
      判断当前线程组是否为指定线程的父线程

    • public boolean isDaemon()
      判断该线程组是否是一个守护线程组。当最后一个线程停止或最后一个线程组销毁时,该守护线程组会自动销毁。

    • public void list()
      列出线程组及线程组中的各个线程的信息。

      /*
      等价于
      System.out.println(threadGroup.toString());
      System.out.println("\t"+thread.toString());
      ……
      */
      java.lang.ThreadGroup"线程组名", maxpri="组内线程的最大优先级"]
       Thread[线程名, 线程优先级, 直接隶属的线程组名]
       ……
    • public final void resume()
      使当前组内被挂起的线程恢复到可运行状态

    • public final void setDaemon (boolean daemon)
      设置该线程组是否是守护线程组。如果是守护线程组的话,当最后一个线程停止或最后一个线程组销毁时,该守护线程组会自动销毁。(注意:线程组设置为守护线程并不会改变线程组内的线程是否是守护线程)

    • public final void stop()
      终止当前线程组中所有线程

    • public final void suspend()
      挂起当前线程组中所有线程

    • public String toStrinng()
      将当前线程组转换为String对象,输出格式为

      java.lang.ThreadGroup[name="线程组名", maxpri="组内线程的最大优先级"]

代码实战

package com.demo.test;

public class ThreadGroupDemo {
    private static ThreadGroup threadGroup;

    public static void main(String[] args) {
        threadGroup = new ThreadGroup("threadGroup");
        threadGroup.setDaemon(true);
        int len = 10;
        for (int i = 0; i < len; i++) {
            final int index = i;
            Thread thread = new Thread(threadGroup, new Runnable() {

                @Override
                public void run() {
                    try {
                        Thread.sleep(100 * index);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    System.out.println("activeCount = " + threadGroup.activeCount());
                    System.out.println("activeGroupCount = " + threadGroup.activeGroupCount());

                    Thread[] threads = new Thread[threadGroup.activeCount()];
                    boolean recurse = false;
                    threadGroup.enumerate(threads, recurse);
                    System.out.println("enumerate(threads, recurse) = ");
                    for (int j = 0; j < threads.length; j++) {
                        System.out.println("\tthreads[" + j + "]=" + threads[j].toString() + ", isDaemon=" + threads[j].isDaemon());
                    }
                    System.out.println("getMaxPriority = " + threadGroup.getMaxPriority());
                    System.out.println("isDaemon = " + threadGroup.isDaemon());
                    System.out.println("list = ");
                    threadGroup.list();

                    System.out.println("toString = " + threadGroup.toString());
                    System.out.println("checkAccess");
                    try {
                        threadGroup.checkAccess();
                    } catch (SecurityException e) {
                        e.printStackTrace();
                    }
                    System.out.println("----------------------");
                }
            }, "thread_" + i);
            /*
             * public final static int MIN_PRIORITY = 1;
             * public final static int NORM_PRIORITY = 5;
             * public final static int MAX_PRIORITY = 10;
             */
            int priority = i;
            if (priority < Thread.MIN_PRIORITY) {
                priority = Thread.MIN_PRIORITY;
            } else if (priority > Thread.MAX_PRIORITY) {
                priority = Thread.MAX_PRIORITY;
            }
            thread.setPriority(priority);
            thread.start();
        }
    }
}

运行截图

这里写图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值