日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java并发编程之ThreadGroup

發布時間:2023/12/19 java 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java并发编程之ThreadGroup 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

ThreadGroup是Java提供的一種對線程進行分組管理的手段,可以對所有線程以組為單位進行操作,如設置優先級、守護線程等。

線程組也有父子的概念,如下圖:

線程組的創建

1 public class ThreadGroupCreator { 2 3 public static void main(String[] args) { 4 //獲取當前線程的group 5 ThreadGroup currentGroup = Thread.currentThread().getThreadGroup(); 6 //在當前線程執行流中新建一個Group1 7 ThreadGroup group1 = new ThreadGroup("Group1"); 8 //Group1的父線程,就是main線程所在Group 9 System.out.println(group1.getParent() == currentGroup); 10 //定義Group2, 指定group1為其父線程 11 ThreadGroup group2 = new ThreadGroup(group1, "Group2"); 12 System.out.println(group2.getParent() == group1); 13 } 14 }

線程組的基本操作

注意:后添加進線程組的線程,其優先級不能大于線程組的優先級

1 public class ThreadGroupBasic { 2 3 public static void main(String[] args) throws InterruptedException { 4 5 ThreadGroup group = new ThreadGroup("group1"); 6 Thread thread = new Thread(group, () -> { 7 while(true) { 8 try { 9 TimeUnit.SECONDS.sleep(1); 10 } catch (InterruptedException e) { 11 e.printStackTrace(); 12 } 13 } 14 }, "thread"); 15 thread.setDaemon(true); 16 thread.start(); 17 18 TimeUnit.MILLISECONDS.sleep(1); 19 20 ThreadGroup mainGroup = Thread.currentThread().getThreadGroup(); 21 //遞歸獲取mainGroup中活躍線程的估計值 22 System.out.println("activeCount = " + mainGroup.activeCount()); 23 //遞歸獲mainGroup中的活躍子group 24 System.out.println("activeGroupCount = " + mainGroup.activeGroupCount()); 25 //獲取group的優先級, 默認為10 26 System.out.println("getMaxPriority = " + mainGroup.getMaxPriority()); 27 //獲取group的名字 28 System.out.println("getName = " + mainGroup.getName()); 29 //獲取group的父group, 如不存在則返回null 30 System.out.println("getParent = " + mainGroup.getParent()); 31 //活躍線程信息全部輸出到控制臺 32 mainGroup.list(); 33 System.out.println("----------------------------"); 34 //判斷當前group是不是給定group的父線程, 如果兩者一樣,也會返回true 35 System.out.println("parentOf = " + mainGroup.parentOf(group)); 36 System.out.println("parentOf = " + mainGroup.parentOf(mainGroup)); 37 38 } 39 40 }

線程組的Interrupt

1 ublic class ThreadGroupInterrupt { 2 3 public static void main(String[] args) throws InterruptedException { 4 ThreadGroup group = new ThreadGroup("TestGroup"); 5 new Thread(group, () -> { 6 while(true) { 7 try { 8 TimeUnit.MILLISECONDS.sleep(2); 9 } catch (InterruptedException e) { 10 //received interrupt signal and clear quickly 11 System.out.println(Thread.currentThread().isInterrupted()); 12 break; 13 } 14 } 15 System.out.println("t1 will exit"); 16 }, "t1").start(); 17 new Thread(group, () -> { 18 while(true) { 19 try { 20 TimeUnit.MILLISECONDS.sleep(2); 21 } catch (InterruptedException e) { 22 //received interrupt signal and clear quickly 23 System.out.println(Thread.currentThread().isInterrupted()); 24 break; 25 } 26 } 27 System.out.println("t2 will exit"); 28 }, "t2").start(); 29 //make sure all threads start 30 TimeUnit.MILLISECONDS.sleep(2); 31 32 group.interrupt(); 33 } 34 35 }

線程組的destroy

1 public class ThreadGroupDestroy { 2 3 public static void main(String[] args) { 4 ThreadGroup group = new ThreadGroup("TestGroup"); 5 ThreadGroup mainGroup = Thread.currentThread().getThreadGroup(); 6 //before destroy 7 System.out.println("group.isDestroyed=" + group.isDestroyed()); 8 mainGroup.list(); 9 10 group.destroy(); 11 //after destroy 12 System.out.println("group.isDestroyed=" + group.isDestroyed()); 13 mainGroup.list(); 14 } 15 16 }

線程組設置守護線程組

線程組設置為守護線程組,并不會影響其線程是否為守護線程,僅僅表示當它內部沒有active的線程的時候,會自動destroy

?

1 public class ThreadGroupDaemon { 2 3 public static void main(String[] args) throws InterruptedException { 4 ThreadGroup group1 = new ThreadGroup("group1"); 5 new Thread(group1, () -> { 6 try { 7 TimeUnit.SECONDS.sleep(1); 8 } catch (InterruptedException e) { 9 e.printStackTrace(); 10 } 11 }, "group1-thread1").start(); 12 ThreadGroup group2 = new ThreadGroup("group2"); 13 new Thread(group2, () -> { 14 try { 15 TimeUnit.SECONDS.sleep(1); 16 } catch (InterruptedException e) { 17 e.printStackTrace(); 18 } 19 }, "group1-thread2").start(); 20 group2.setDaemon(true); 21 22 TimeUnit.SECONDS.sleep(3); 23 System.out.println(group1.isDestroyed()); 24 System.out.println(group2.isDestroyed()); 25 } 26 }

?

總結

以上是生活随笔為你收集整理的Java并发编程之ThreadGroup的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。