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
發佈了126 篇原創文章 · 獲贊 46 · 訪問量 29萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章