- 線程組介紹
- 線程組的構(gòu)造
- ThreadGroup方法介紹
- 查看線程組信息
- 終止線程組中的所有線程
- 總結(jié)
- Links
- 作者資源
- 相關(guān)資源
線程組介紹
線程組(ThreadGroup)簡(jiǎn)單來說就是一個(gè)線程集合。線程組的出現(xiàn)是為了更方便地管理線程。
線程組是父子結(jié)構(gòu)的,一個(gè)線程組可以集成其他線程組,同時(shí)也可以擁有其他子線程組。從結(jié)構(gòu)上看,線程組是一個(gè)樹形結(jié)構(gòu),每個(gè)線程都隸屬于一個(gè)線程組,線程組又有父線程組,這樣追溯下去,可以追溯到一個(gè)根線程組——System線程組。

下面介紹一下線程組樹的結(jié)構(gòu):
- JVM創(chuàng)建的system線程組是用來處理JVM的系統(tǒng)任務(wù)的線程組,例如對(duì)象的銷毀等。
- system線程組的直接子線程組是main線程組,這個(gè)線程組至少包含一個(gè)main線程,用于執(zhí)行main方法。
- main線程組的子線程組就是應(yīng)用程序創(chuàng)建的線程組。
你可以在main方法中看到JVM創(chuàng)建的system線程組和main線程組:
public static void main(String[] args) {
ThreadGroup mainThreadGroup=Thread.currentThread().getThreadGroup();
ThreadGroup systenThreadGroup=mainThreadGroup.getParent();
System.out.println("systenThreadGroup name = "+systenThreadGroup.getName());
System.out.println("mainThreadGroup name = "+mainThreadGroup.getName());
}
console輸出:
systenThreadGroup name = system
mainThreadGroup name = main
一個(gè)線程可以訪問其所屬線程組的信息,但不能訪問其所屬線程組的父線程組或者其他線程組的信息。
線程組的構(gòu)造
java.lang.ThreadGroup提供了兩個(gè)構(gòu)造函數(shù):
| Constructor | Description |
|---|---|
| ThreadGroup(String name) | 根據(jù)線程組名稱創(chuàng)建線程組,其父線程組為main線程組 |
| ThreadGroup(ThreadGroup parent, String name) | 根據(jù)線程組名稱創(chuàng)建線程組,其父線程組為指定的parent線程組 |
下面演示一下這兩個(gè)構(gòu)造函數(shù)的用法:
public static void main(String[] args) {
ThreadGroup subThreadGroup1 = new ThreadGroup("subThreadGroup1");
ThreadGroup subThreadGroup2 = new ThreadGroup(subThreadGroup1, "subThreadGroup2");
System.out.println("subThreadGroup1 parent name = " + subThreadGroup1.getParent().getName());
System.out.println("subThreadGroup2 parent name = " + subThreadGroup2.getParent().getName());
}
console輸出:
subThreadGroup1 parent name = main
subThreadGroup2 parent name = subThreadGroup1
ThreadGroup方法介紹
ThreadGroup提供了很多有用的方法,下面提供了這些方法的簡(jiǎn)要介紹,以及部分方法的使用示例。
| S.N. | Method | Description |
|---|---|---|
| 1) | void checkAccess() | This method determines if the currently running thread has permission to modify the thread group. |
| 2) | int activeCount() | This method returns an estimate of the number of active threads in the thread group and its subgroups. |
| 3) | int activeGroupCount() | This method returns an estimate of the number of active groups in the thread group and its subgroups. |
| 4) | void destroy() | This method destroys the thread group and all of its subgroups. |
| 5) | int enumerate(Thread[] list) | This method copies into the specified array every active thread in the thread group and its subgroups. |
| 6) | int getMaxPriority() | This method returns the maximum priority of the thread group. |
| 7) | String getName() | This method returns the name of the thread group. |
| 8) | ThreadGroup getParent() | This method returns the parent of the thread group. |
| 9) | void interrupt() | This method interrupts all threads in the thread group. |
| 10) | boolean isDaemon() | This method tests if the thread group is a daemon thread group. |
| 11) | void setDaemon(boolean daemon) | This method changes the daemon status of the thread group. |
| 12) | boolean isDestroyed() | This method tests if this thread group has been destroyed. |
| 13) | void list() | This method prints information about the thread group to the standard output. |
| 14) | boolean parentOf(ThreadGroup g) | This method tests if the thread group is either the thread group argument or one of its ancestor thread groups. |
| 15) | void suspend() | This method is used to suspend all threads in the thread group. |
| 16) | void resume() | This method is used to resume all threads in the thread group which was suspended using suspend() method. |
| 17) | void setMaxPriority(int pri) | This method sets the maximum priority of the group. |
| 18) | void stop() | This method is used to stop all threads in the thread group. |
| 19) | String toString() | This method returns a string representation of the Thread group. |
查看線程組信息
下面演示了查看當(dāng)前線程組的信息。
public static void list(){
ThreadGroup tg = new ThreadGroup ("subgroup 1");
Thread t1 = new Thread (tg, "thread 1");
Thread t2 = new Thread (tg, "thread 2");
Thread t3 = new Thread (tg, "thread 3");
tg = new ThreadGroup ("subgroup 2");
Thread t4 = new Thread (tg, "my thread");
tg = Thread.currentThread ().getThreadGroup ();
int agc = tg.activeGroupCount ();
System.out.println ("Active thread groups in " + tg.getName () + " thread group: " + agc);
tg.list ();
}
輸出如下:
Active thread groups in main thread group: 2
java.lang.ThreadGroup[name=main,maxpri=10]
Thread[main,5,main]
java.lang.ThreadGroup[name=subgroup 1,maxpri=10]
java.lang.ThreadGroup[name=subgroup 2,maxpri=10]
終止線程組中的所有線程
一個(gè)線程應(yīng)由其他線程來強(qiáng)制中斷或停止,而是應(yīng)該由線程自己自行停止。
因此 Thread.currentThread().stop(), Thread.currentThread().suspend(), Thread.currentThread().resume() 都已經(jīng)被廢棄了。
interrupt() 方法的作用是通知線程應(yīng)該中斷了,具體到底中斷還是繼續(xù)運(yùn)行,由被通知的線程處理。
public class ThreadGroupExampleInterrupt {
public static void main(String[] args) {
// Start two threads
MyThread mt = new MyThread();
mt.setName("A");
mt.start();
mt = new MyThread();
mt.setName("B");
mt.start();
// Wait 2 seconds
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Interrupt all methods in the same thread group as the main thread
Thread.currentThread().getThreadGroup().interrupt();
}
//一個(gè)啟動(dòng)以后進(jìn)入等待,直到被interrupt的線程
static class MyThread extends Thread {
public void run() {
synchronized ("A") {
System.out.println(getName() + " about to wait.");
try {
"A".wait();
} catch (InterruptedException e) {
System.out.println(getName() + " interrupted.");
}
System.out.println(getName() + " terminating.");
}
}
}
}
執(zhí)行main方法輸出:
A about to wait.
B about to wait.
A interrupted.
A terminating.
B interrupted.
B terminating.
總結(jié)
本節(jié)介紹了線程組(ThreadGroup)的概念,及其結(jié)構(gòu)和構(gòu)造函數(shù),并演示了使用線程組方便地管理組內(nèi)線程的幾個(gè)方法。
本節(jié)是并發(fā)系列教程的一節(jié),更多相關(guān)教程可以訪問文章后面的鏈接。
后續(xù)會(huì)有更多關(guān)于并發(fā)編程的知識(shí)點(diǎn)的介紹,并且會(huì)結(jié)合企業(yè)項(xiàng)目進(jìn)行實(shí)戰(zhàn)介紹,歡迎繼續(xù)關(guān)注。