多執行緒 之 執行緒組(ThreadGroup)和執行緒組的中斷
阿新 • • 發佈:2019-01-10
class Demo implements Runnable { public synchronized void run() { try{wait();} catch (InterruptedException e) { System.out.println(Thread.currentThread().getName()+"已經停止等待 丟擲異常"); } for(int i=0;i<10;i++) System.out.println(Thread.currentThread()+"..."+i); } } public class Main { public static void main(String[] args) { Demo d1=new Demo(); Demo d2=new Demo(); Demo d3=new Demo(); //預設的執行緒組是main ThreadGroup t=new ThreadGroup("執行緒組t"); Thread t1=new Thread(t,d1); Thread t2=new Thread(t,d2); Thread t3=new Thread(t,d3); t1.start(); t2.start(); t3.start(); t.interrupt(); } }
執行結果: