Java併發程式設計之ThreadGroup
阿新 • • 發佈:2018-11-08
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 }