多线程学习笔记(一)--wait() notifyall()

public class Car {

    private boolean waxOn=false;
    
    public synchronized void waxed(){
        waxOn=true;
        System.out.println("waxed notifyall start");
        notifyAll();
        System.out.println("waxed notifyall end");
    }
    
    public synchronized void buffed(){
        waxOn=false;
        System.out.println("buffed notifyall start");
        notifyAll();
        System.out.println("buffed notifyall end");
    }
    
    public synchronized void waitForWaxing() throws InterruptedException{
        while(false==waxOn){
            System.out.println("waitForWaxing wait start");
            wait();
            System.out.println("waitForWaxing wait end");
        }
    }
    
    public synchronized void waitForBuffing() throws InterruptedException{
        while(true==waxOn){
            System.out.println("waitForBuffing wait start");
            wait();
            System.out.println("waitForEnd wait end");
        }
    }
    
    
    public static void main(String[] args) {
        Car car=new Car();
        ExecutorService ex=Executors.newCachedThreadPool();
        ex.execute(new WaxOn(car));
        ex.execute(new WaxOff(car));
        
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        ex.shutdownNow();
    }
}

class WaxOn implements Runnable{

    Car car;
    public WaxOn(Car car){
        this.car=car;
    }
    @Override
    public void run() {
        
        try {
            while (!Thread.interrupted()) {
                System.out.println("Wax on !");
                TimeUnit.MILLISECONDS.sleep(200);
                System.out.println("Wax on notifyall");
                car.waxed();
                System.out.println("Wax on wait");
                car.waitForBuffing();
        
                
            }
        } catch (InterruptedException e) {
            System.out.println("WaxOn InterruptedException");
            //e.printStackTrace();
        }
        System.out.println("WaxOn wax run over");
        
    }
    
}

class WaxOff implements Runnable{
    
    Car car;
    
    public WaxOff(Car car){
        this.car=car;
    }
    @Override
    public void run() {
        try {
            while (!Thread.interrupted()) {
                System.out.println("Wax off wait");
                car.waitForWaxing();
                System.out.println("Wax off !");
                TimeUnit.MILLISECONDS.sleep(200);
                System.out.println("Wax off notifyall");
                car.buffed();
            
        
            }
        } catch (InterruptedException e) {
            System.out.println("WaxOff InterruptedException");
            //e.printStackTrace();
        }
        System.out.println("WaxOff wax run over");
        
    }
    
    
}
输出:
Wax on !
Wax off wait
waitForWaxing wait start
Wax on notifyall
waxed notifyall start
waxed notifyall end
Wax on wait
waitForWaxing wait end
Wax off !
waitForBuffing wait start
Wax off notifyall
buffed notifyall start
buffed notifyall end
waitForEnd wait end
-------------------------------------------------------
Wax on !
Wax off wait
waitForWaxing wait start
Wax on notifyall
waxed notifyall start
waxed notifyall end
Wax on wait
waitForWaxing wait end
waitForBuffing wait start
Wax off !
Wax off notifyall
buffed notifyall start
buffed notifyall end
Wax off wait
waitForWaxing wait start
waitForEnd wait end
--------------------------------------------------------
Wax on !
Wax on notifyall
waxed notifyall start
waxed notifyall end
Wax on wait
waitForWaxing wait end
Wax off !
waitForBuffing wait start
Wax off notifyall
buffed notifyall start
buffed notifyall end
Wax off wait
waitForEnd wait end
--------------------------------------------------------
Wax on !
waitForWaxing wait start
Wax on notifyall
waxed notifyall start
waxed notifyall end
Wax on wait
waitForWaxing wait end
Wax off !
waitForBuffing wait start
Wax off notifyall
buffed notifyall start
buffed notifyall end
Wax off wait
waitForEnd wait end
----------------------------------------------------------
Wax on !
waitForWaxing wait start
Wax on notifyall
waxed notifyall start
waxed notifyall end
Wax on wait
waitForBuffing wait start
waitForWaxing wait end
Wax off !
Wax off notifyall
buffed notifyall start
buffed notifyall end
Wax off wait
waitForEnd wait end
------------------------------------------------------------
Wax on !
waitForWaxing wait start
Wax on notifyall
waxed notifyall start
waxed notifyall end
Wax on wait
waitForBuffing wait start
waitForWaxing wait end
Wax off !
Wax off notifyall
buffed notifyall start
buffed notifyall end
Wax off wait
waitForEnd wait end
---------------------------------------------------------------
Wax on !
waitForWaxing wait start
Wax on notifyall
waxed notifyall start
waxed notifyall end
Wax on wait
waitForWaxing wait end
Wax off !
waitForBuffing wait start
Wax off notifyall
buffed notifyall start
buffed notifyall end
Wax off wait
waitForEnd wait end
---------------------------------------------------------------
Wax on !
waitForWaxing wait start
WaxOn InterruptedException
WaxOn wax run over
WaxOff InterruptedException
WaxOff wax run over
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容