美文网首页
多线程学习笔记(一)--wait() notifyall()

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

作者: laifrog | 来源:发表于2022-12-10 16:24 被阅读0次
    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
    

    相关文章

      网友评论

          本文标题:多线程学习笔记(一)--wait() notifyall()

          本文链接:https://www.haomeiwen.com/subject/aoamjxtx.html