Java多線程(十一):線程組

線程組

線程組能夠批量管理線程和線程組對象。java

一級關聯

例子以下,創建一級關聯。this

public class MyThread43 implements Runnable{
    public void run()
    {
        try
        {
            while (!Thread.currentThread().isInterrupted())
            {
                System.out.println("ThreadName = " + Thread.currentThread().getName());
                Thread.sleep(3000);
            }
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }
    }

    public static void main(String[] args)
    {
        MyThread43 mt0 = new MyThread43();
        MyThread43 mt1 = new MyThread43();
        ThreadGroup tg = new ThreadGroup("新建線程組1");
        Thread t0 = new Thread(tg, mt0);
        Thread t1 = new Thread(tg, mt1);
        t0.start();
        t1.start();
        System.out.println("活動的線程數爲:" + tg.activeCount());
        System.out.println("線程組的名稱爲:" + tg.getName());
    }
}

輸出結果以下線程

活動的線程數爲:2
線程組的名稱爲:新建線程組1
ThreadName = Thread-0
ThreadName = Thread-1
ThreadName = Thread-0
ThreadName = Thread-1
ThreadName = Thread-1
ThreadName = Thread-0
ThreadName = Thread-1
ThreadName = Thread-0
······

每隔三秒輸出兩個線程名稱,符合預期。code

線程組自動歸組屬性

public class ThreadDomain49 {
    public static void main(String[] args) {
        System.out.println("A處線程:" + Thread.currentThread().getName() + ", 所屬線程:" + Thread.currentThread().getThreadGroup().getName() +
                ", 組中有線程組數量:" + Thread.currentThread().getThreadGroup().activeGroupCount());
        ThreadGroup group = new ThreadGroup("新的組");
        System.out.println("B處線程:" + Thread.currentThread().getName() + ", 所屬線程:" + Thread.currentThread().getThreadGroup().getName() +
                ", 組中有線程組數量:" + Thread.currentThread().getThreadGroup().activeGroupCount());
        ThreadGroup[] tg = new ThreadGroup[Thread.currentThread().getThreadGroup().activeGroupCount()];
        Thread.currentThread().getThreadGroup().enumerate(tg);
        for (int i = 0; i < tg.length; i++)
            System.out.println("第一個線程組名稱爲:" + tg[i].getName());
    }
}

輸出結果以下對象

A處線程:main, 所屬線程:main, 組中有線程組數量:0
B處線程:main, 所屬線程:main, 組中有線程組數量:1
第一個線程組名稱爲:新的組

沒有指定線程組,則歸屬到當前線程所屬的組。blog

根線程組

public class ThreadDomain50 {


    public static void main(String[] args)
    {
        System.out.println(Thread.currentThread().getThreadGroup().getParent().getName());
        System.out.println(Thread.currentThread().getThreadGroup().getParent().getParent().getName());
    }


}

運行結果get

system
Exception in thread "main" java.lang.NullPointerException
    at com.advance.MultiThread3.MyThread.ThreadDomain50.main(ThreadDomain50.java:14)

當前線程的線程組的父線程組是系統線程組;系統線程組的父線程組不存在;系統線程組就是根線程組。io

批量中止組內線程
請看示例class

public class MyThread44 extends Thread{

    public MyThread44(ThreadGroup tg, String name)
    {
        super(tg, name);
    }

    public void run()
    {
        System.out.println("ThreadName = " + Thread.currentThread().getName() +
                "準備開始死循環了");
        while (!this.isInterrupted()){}
        System.out.println("ThreadName = " + Thread.currentThread().getName() +
                "結束了");
    }

    public static void main(String[] args) throws InterruptedException {
        ThreadGroup tg = new ThreadGroup("個人線程組");
        MyThread44 mt = null;
        for (int i = 0; i < 3; i++)
        {
            mt = new MyThread44(tg, "線程" + i);
            mt.start();
        }
        Thread.sleep(5000);
        tg.interrupt();
        System.out.println("調用了interrupt()方法");
    }
}

輸出結果以下thread

ThreadName = 線程0準備開始死循環了
ThreadName = 線程1準備開始死循環了
ThreadName = 線程2準備開始死循環了
調用了interrupt()方法
ThreadName = 線程0結束了
ThreadName = 線程2結束了
ThreadName = 線程1結束了

能夠看到,ThreadGroup的interrupt方法批量中斷線程組的線程。

相關文章
相關標籤/搜索