标签:port bad oid sys 检查 priority sed 构造 closed
使用:
1 import javax.swing.text.html.HTMLDocument.HTMLReader.IsindexAction; 2 3 public class Test { 4 5 public static void main(String[] args) { 6 7 ThreadGroup tg = new ThreadGroup("threadGroup-001"); 8 9 Thread t1 = new Thread(tg, new MyThread()); 10 t1.start(); 11 12 Thread t2 = new Thread(tg, new MyThread()); 13 t2.start(); 14 15 // 返回线程组中活动线程的估计数 16 System.out.println("active thread group: " + tg.activeCount()); 17 // 返回此线程组中活动线程组的估计数 18 System.out.println("activeGroupCount: " + tg.activeGroupCount()); 19 // 检查当前运行的线程是否有权修改此线程组 20 tg.checkAccess(); 21 // 设置线程组的最高优先级 22 tg.setMaxPriority(6); 23 // 返回此线程组的最高优先级 24 System.out.println("maxPriority: " + tg.getMaxPriority()); 25 // 返回此线程组的名称 26 System.out.println("thread group name: " + tg.getName()); 27 // 返回此线程组的父线程组 28 System.out.println(tg.getParent()); 29 // 中断此线程组中的所有线程 30 tg.interrupt(); 31 // 更改此线程组的后台程序状态 32 tg.setDaemon(true); 33 // 测试此线程组是否为一个后台程序线程组 34 System.out.println("is daemon: " + tg.isDaemon()); 35 // 测试此线程组是否为线程组参数或其祖先线程组之一 36 System.out.println("is parent: "+ tg.getParent().parentOf(tg)); 37 // 打印线程组信息 38 tg.list(); 39 // 返回线程组的字符串表示形式 40 System.out.println(tg.toString()); 41 // 销毁此线程组及其所有子组 42 tg.destroy(); 43 // 测试此线程组是否已经销毁 44 System.out.println(tg.isDestroyed()); 45 // System.out.println(tg.); 46 } 47 48 private static class MyThread extends Thread { 49 @Override 50 public void run() { 51 System.out.println("thread name: " + Thread.currentThread().getName()); 52 } 53 } 54 55 }
一、构造函数
两种构造函数:
ThreadGroup(String name)
ThreadGroup(ThreadGroup parent, String name)
public ThreadGroup(String name) { this(Thread.currentThread().getThreadGroup(), name); }
1 public ThreadGroup(ThreadGroup parent, String name) { 2 this(checkParentAccess(parent), parent, name); 3 }
1 private ThreadGroup(Void unused, ThreadGroup parent, String name) { 2 this.name = name; 3 this.maxPriority = parent.maxPriority; 4 this.daemon = parent.daemon; 5 this.parent = parent; 6 parent.add(this); 7 }
1 private final void add(ThreadGroup g){ 2 synchronized (this) { 3 if (destroyed) { 4 throw new IllegalThreadStateException(); 5 } 6 // 添加一个线程组到此线程组的groups数组中,groups初始容量为4,每次容量耗尽之后按2倍扩增。 7 if (groups == null) { 8 groups = new ThreadGroup[4]; 9 } else if (ngroups == groups.length) { 10 groups = Arrays.copyOf(groups, ngroups * 2); 11 } 12 groups[ngroups] = g; 13 14 // This is done last so it doesn‘t matter in case the 15 // thread is killed 16 ngroups++; 17 } 18 }
标签:port bad oid sys 检查 priority sed 构造 closed
原文地址:https://www.cnblogs.com/natian-ws/p/10195184.html