Java中使用線程組的範例和方法介紹
Java中執行緒組(ThreadGroup類別)
Java中使用ThreadGroup類別來代表執行緒群組,表示一組執行緒的集合,可以對一批執行緒和執行緒群組進行管理。可以把線程歸屬到某一個線程組中,線程組中可以有線程對象,也可以有線程組,組中還可以有線程,這樣的組織結構有點類似樹的形式,如圖所示。
# 使用者建立的所有執行緒都屬於指定執行緒組,如果沒有明確指定屬於哪個執行緒組,那麼該執行緒就屬於預設執行緒組(即main執行緒組)。預設情況下,子執行緒和父執行緒處於同一個執行緒組。
此外,只有在建立執行緒時才能指定其所在的執行緒組,執行緒運行中途不能改變它所屬的執行緒組,也就是說一旦指定所在的執行緒組就不能改變。
二.為什麼要使用線程組
1.安全性
同一個線程組的線程是可以互相修改對方的資料的。但如果在不同的線程組中,那麼就不能「跨線程組」修改數據,可以從一定程度上確保數據安全。
2.批量管理
可以批次管理執行緒或執行緒組對象,有效地對執行緒或執行緒組對象進行組織或控制。
三.線程組使用範例
1.執行緒關聯執行緒組:一級關聯
# 所謂一級關聯就是父對像中有子對象,但並沒有創建孫對象。例如建立一個線程組,然後將創建的線程歸屬到該組中,從而對這些線程進行有效的管理。程式碼範例如下:
public class ThreadGroupTest { public static void main(String[] args) { ThreadGroup rootThreadGroup = new ThreadGroup("root线程组"); Thread thread0 = new Thread(rootThreadGroup, new MRunnable(), "线程A"); Thread thread1 = new Thread(rootThreadGroup, new MRunnable(), "线程B"); thread0.start(); thread1.start(); } } class MRunnable implements Runnable { @Override public void run() { while (!Thread.currentThread().isInterrupted()) { System.out.println("线程名: " + Thread.currentThread().getName() + ", 所在线程组: " + Thread.currentThread().getThreadGroup().getName()) ; try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } } 复制代码
執行結果如下:
线程名: 线程A, 所在线程组: root线程组 线程名: 线程B, 所在线程组: root线程组 复制代码
2.執行緒關聯執行緒組:多層級關聯
# 所謂的多層關聯就是父對像中有子對象,子對像中再創建孫對像也就出現了子孫的效果了。例如使用下圖第二個建構方法,將子執行緒組歸屬到某個執行緒組,再將建立的執行緒歸屬到子執行緒組,這樣就會有執行緒樹的效果了。
# 程式碼範例如下:
public class ThreadGroupTest { public static void main(String[] args) { ThreadGroup rootThreadGroup = new ThreadGroup("root线程组"); Thread thread0 = new Thread(rootThreadGroup, new MRunnable(), "线程A"); Thread thread1 = new Thread(rootThreadGroup, new MRunnable(), "线程B"); thread0.start(); thread1.start(); ThreadGroup threadGroup1 = new ThreadGroup(rootThreadGroup, "子线程组"); Thread thread2 = new Thread(threadGroup1, new MRunnable(), "线程C"); Thread thread3 = new Thread(threadGroup1, new MRunnable(), "线程D"); thread2.start(); thread3.start(); } } class MRunnable implements Runnable { @Override public void run() { while (!Thread.currentThread().isInterrupted()) { System.out.println("线程名: " + Thread.currentThread().getName() + ", 所在线程组: " + Thread.currentThread().getThreadGroup().getName() + ", 父线程组: " + Thread.currentThread().getThreadGroup().getParent().getName()); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } } 复制代码
執行結果如下:
线程名: 线程A, 所在线程组: root线程组, 父线程组: main 线程名: 线程B, 所在线程组: root线程组, 父线程组: main 线程名: 线程C, 所在线程组: 子线程组, 父线程组: root线程组 线程名: 线程D, 所在线程组: 子线程组, 父线程组: root线程组 复制代码
3.批量管理組內線程
使用線程組自然是要對線程進行批量管理,例如可以批量中斷組內線程,程式碼範例如下:
public class ThreadGroupTest { public static void main(String[] args) { ThreadGroup rootThreadGroup = new ThreadGroup("root线程组"); Thread thread0 = new Thread(rootThreadGroup, new MRunnable(), "线程A"); Thread thread1 = new Thread(rootThreadGroup, new MRunnable(), "线程B"); thread0.start(); thread1.start(); ThreadGroup threadGroup1 = new ThreadGroup(rootThreadGroup, "子线程组"); Thread thread2 = new Thread(threadGroup1, new MRunnable(), "线程C"); Thread thread3 = new Thread(threadGroup1, new MRunnable(), "线程D"); thread2.start(); thread3.start(); rootThreadGroup.interrupt(); System.out.println("批量中断组内线程"); } } class MRunnable implements Runnable { @Override public void run() { while (!Thread.currentThread().isInterrupted()) { System.out.println("线程名: " + Thread.currentThread().getName() + ", 所在线程组: " + Thread.currentThread().getThreadGroup().getName() + ", 父线程组: " + Thread.currentThread().getThreadGroup().getParent().getName()); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); break; } } System.out.println(Thread.currentThread().getName() + "执行结束"); } } 复制代码
執行結果如下:
线程名: 线程A, 所在线程组: root线程组, 父线程组: main 线程名: 线程B, 所在线程组: root线程组, 父线程组: main 线程名: 线程C, 所在线程组: 子线程组, 父线程组: root线程组 线程名: 线程D, 所在线程组: 子线程组, 父线程组: root线程组 批量中断组内线程 线程A执行结束 线程B执行结束 线程C执行结束 线程D执行结束 复制代码
以上是Java中使用線程組的範例和方法介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)