import java.util.concurrent.locks.*; class Do9 { public static void main(String[] args) { Ds d=new Ds(); Thread t1=new Thread(d); Thread t2=new Thread(d); Thread t3=new Thread(d); t1.start(); t2.start(); t3.setDaemon(true);//变成后台线层,要在start()之前 t3.start(); for (int i=0; i<9999;i++ ) { if(i==9997) { t1.interrupt(); t2.interrupt(); break; } System.out.println(i); } } } class Ds implements Runnable { private boolean flag=true; public synchronized void run() { while(flag) { try{wait();}catch(InterruptedException e){System.out.println(Thread.currentThread().getName()+"..."+e);} System.out.println(Thread.currentThread().getName()+"哈哈"); break; } } public void setFlag() { flag=false; } }
时间: 2024-10-14 14:17:46