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();
}
}
運作結果:
