文章分类

首页 / 文章分类 / 正文

Image

ThreadGroup解读

ThreadGroup是位于java.lang包下的一个类,用于统一的线程管理.一旦一个线程加入到一个线程组后,就不能更换线程所在的线程组

将当前线程加入到线程组中

Thread thread = new Thread(threadGroup, new MyThread(), "threadname-" + i);

通过重写uncaughtException方法捕获异常

ThreadGroup threadGroup = new ThreadGroup("test-group") {             @Override             public void uncaughtException(Thread t, Throwable e) {                 System.out.println("ThreadGroup捕获到线程异常 - " + e.getMessage());             }         };

将ThreadGroup中活跃的线程引用复制到线程组

Thread[] threads = new Thread[num];         threadGroup.enumerate(threads);         for (Thread t : threads) {             System.out.println("线程名-" + t.getName());         }

测试源代码如下

public class MyThread implements Runnable {      @Override     public void run() {         try {             System.out.println(Thread.currentThread().getName() + " -> start");             TimeUnit.SECONDS.sleep(10);             //随机发生异常             if (ThreadLocalRandom.current().nextInt(10) > 5) {                 throw new RuntimeException(Thread.currentThread().getName() + "发生异常");             }             System.out.println(Thread.currentThread().getName() + " -> end");         } catch (InterruptedException e) {             e.printStackTrace();         }     } 
public class ThreadGroupTest {     public static void main(String[] args) {         int num = 10;         ThreadGroup threadGroup = new ThreadGroup("test-group") {             @Override             public void uncaughtException(Thread t, Throwable e) {                 System.out.println("ThreadGroup捕获到线程异常 - " + e.getMessage());             }         };          List<Thread> threadList = new ArrayList<>();         for (int i = 0; i < num; i++) {             Thread thread = new Thread(threadGroup, new MyThread(), "threadname-" + i);             threadList.add(thread);         }          System.out.println("运行前线程组中活跃线程数 -> " + threadGroup.activeCount());         System.out.println("开始运行所有线程...");         for (Thread t : threadList) {             t.start();         }         //获取线程组中所有[活动]线程         Thread[] threads = new Thread[num];         threadGroup.enumerate(threads);         for (Thread t : threads) {             System.out.println("线程名-" + t.getName());         }         System.out.println("所有线程运行后,线程组中活跃线程数-" + threadGroup.activeCount());         //不断的查看线程组中活跃的线程数         Thread thread = new Thread(() -> {             int num1;             try {                 while ((num1 = threadGroup.activeCount()) > 0) {                     System.out.println("当前线程组活跃线程数为 -> " + num1);                     TimeUnit.SECONDS.sleep(1);                 }                 System.out.println("All Thread HAS FINISHED");             } catch (InterruptedException e) {                 e.printStackTrace();             }         });         thread.start();     } } 

运行结果如下

运行前线程组中活跃线程数 -> 0 开始运行所有线程... threadname-0 -> start threadname-1 -> start threadname-2 -> start threadname-3 -> start threadname-4 -> start threadname-5 -> start threadname-6 -> start threadname-7 -> start threadname-8 -> start 线程名-threadname-0 threadname-9 -> start 线程名-threadname-1 线程名-threadname-2 线程名-threadname-3 线程名-threadname-4 线程名-threadname-5 线程名-threadname-6 线程名-threadname-7 线程名-threadname-8 线程名-threadname-9 所有线程运行后,线程组中活跃线程数-10 当前线程组活跃线程数为 -> 10 当前线程组活跃线程数为 -> 10 当前线程组活跃线程数为 -> 10 当前线程组活跃线程数为 -> 10 当前线程组活跃线程数为 -> 10 当前线程组活跃线程数为 -> 10 当前线程组活跃线程数为 -> 10 当前线程组活跃线程数为 -> 10 当前线程组活跃线程数为 -> 10 当前线程组活跃线程数为 -> 10 threadname-5 -> end threadname-8 -> end ThreadGroup捕获到线程异常 - threadname-7发生异常 ThreadGroup捕获到线程异常 - threadname-2发生异常 threadname-4 -> end ThreadGroup捕获到线程异常 - threadname-3发生异常 ThreadGroup捕获到线程异常 - threadname-9发生异常 ThreadGroup捕获到线程异常 - threadname-1发生异常 threadname-6 -> end threadname-0 -> end All Thread HAS FINISHED