多线程通信问题

多线程通信问题

notify() 唤醒正在此对象监视器上等待的单个线程。
wait() 导致当前线程等待它被唤醒,通常是 通知或 中断 。
wait​(long timeoutMillis) 导致当前线程等待它被唤醒,通常是 通知或 中断 ,或者直到经过一定量的实时。
wait​(long timeoutMillis, int nanos) 导致当前线程等待它被唤醒,通常是 通知或 中断 ,或者直到经过一定量的实时。

两个线程,睡着、唤醒,相互交互执行任务

package xc;

public class Demo11 {
    
    
    public static void main(String[] args) {
    
    
        Food f = new Food();
        new Cook(f).start();
        new Waiter(f).start();
    }


    //厨师
    static class Cook extends Thread{
    
    
        //循环生成100份菜
        private Food f;
        public Cook(Food f){
    
    
            this.f = f;
        }

        @Override
        public void run() {
    
    
            for (int i = 0; i < 100; i++) {
    
    
                if(i%2 == 0){
    
    
                    f.setNameAndTaste("小米粥","甜的");
                }else {
    
    
                    f.setNameAndTaste("麻辣烫","辣的");
                }
            }
        }
    }

    //服务员
    static class Waiter extends Thread{
    
    
        private Food f;
        public Waiter(Food f){
    
    
            this.f = f;
        }

        @Override
        public void run() {
    
    
            for (int i = 0; i < 100; i++) {
    
    
                try {
    
    
                    Thread.sleep(100);
                } catch (InterruptedException e) {
    
    
                    e.printStackTrace();
                }
                f.get();
            }
        }
    }
    //食物

    /**
     * 相当于一个盘子,只new一个对象
     */
    static class Food{
    
    
        private String name;
        private String taste;
        public void setNameAndTaste(String name,String taste){
    
    
            this.name = name;
            //中间时间篇可能会丢,为了更容易出问题,加上休眠
            try {
    
    
                Thread.sleep(100);
            } catch (InterruptedException e) {
    
    
                e.printStackTrace();
            }
            this.taste = taste;
        }
        public void get(){
    
    
            System.out.println("name:"+name+",taste:"+taste);
        }
    }
}

结果:
程序错乱了

产生错误的原因:厨师在set方法执行时,100毫秒后时间篇被服务员抢到,直接将上一个味道和现在的菜端了出去

将food方法里加上synchronized结果会怎么样呢

package xc;

public class Demo11 {
    
    
    public static void main(String[] args) {
    
    
        Food f = new Food();
        new Cook(f).start();
        new Waiter(f).start();
    }


    //厨师
    static class Cook extends Thread{
    
    
        //循环生成100份菜
        private Food f;
        public Cook(Food f){
    
    
            this.f = f;
        }

        @Override
        public void run() {
    
    
            for (int i = 0; i < 100; i++) {
    
    
                if(i%2 == 0){
    
    
                    f.setNameAndTaste("小米粥","甜的");
                }else {
    
    
                    f.setNameAndTaste("麻辣烫","辣的");
                }
            }
        }
    }

    //服务员
    static class Waiter extends Thread{
    
    
        private Food f;
        public Waiter(Food f){
    
    
            this.f = f;
        }

        @Override
        public void run() {
    
    
            for (int i = 0; i < 100; i++) {
    
    
                try {
    
    
                    Thread.sleep(100);
                } catch (InterruptedException e) {
    
    
                    e.printStackTrace();
                }
                f.get();
            }
        }
    }
    //食物

    /**
     * 相当于一个盘子,只new一个对象
     */
    static class Food{
    
    
        private String name;
        private String taste;
        public synchronized void setNameAndTaste(String name,String taste){
    
    
            this.name = name;
            //中间时间篇可能会丢,为了更容易出问题,加上休眠
            try {
    
    
                Thread.sleep(100);
            } catch (InterruptedException e) {
    
    
                e.printStackTrace();
            }
            this.taste = taste;
        }
        public synchronized void get(){
    
    
            System.out.println("name:"+name+",taste:"+taste);
        }
    }
}

结果:
在这里插入图片描述

显然更不行了,因为回首掏的几率更大,有可能服务员一连送好几餐

加上wait()方法,确保下一次执行的对象
加一个状态,看此时是谁在操作Food,true是厨师生产(防止回首掏)

package xc;

public class Demo11 {
    
    
    public static void main(String[] args) {
    
    
        Food f = new Food();
        new Cook(f).start();
        new Waiter(f).start();
    }


    //厨师
    static class Cook extends Thread{
    
    
        //循环生成100份菜
        private Food f;
        public Cook(Food f){
    
    
            this.f = f;
        }

        @Override
        public void run() {
    
    
            for (int i = 0; i < 100; i++) {
    
    
                if(i%2 == 0){
    
    
                    f.setNameAndTaste("小米粥","甜的");
                }else {
    
    
                    f.setNameAndTaste("麻辣烫","辣的");
                }
            }
        }
    }

    //服务员
    static class Waiter extends Thread{
    
    
        private Food f;
        public Waiter(Food f){
    
    
            this.f = f;
        }

        @Override
        public void run() {
    
    
            for (int i = 0; i < 100; i++) {
    
    
                try {
    
    
                    Thread.sleep(100);
                } catch (InterruptedException e) {
    
    
                    e.printStackTrace();
                }
                f.get();
            }
        }
    }
    //食物

    /**
     * 相当于一个盘子,只new一个对象
     */
    static class Food{
    
    
        private String name;
        private String taste;
        //加一个状态,看此时是谁在操作Food,true是厨师生产
        private boolean flag = true;

        public synchronized void setNameAndTaste(String name,String taste){
    
    
            if(flag) {
    
    
                this.name = name;
                //中间时间篇可能会丢,为了更容易出问题,加上休眠
                try {
    
    
                    Thread.sleep(100);
                } catch (InterruptedException e) {
    
    
                    e.printStackTrace();
                }
                this.taste = taste;
                flag = false;
                this.notifyAll();
                try {
    
    
                    this.wait();
                } catch (InterruptedException e) {
    
    
                    e.printStackTrace();
                }
            }
        }
        public synchronized void get(){
    
    
            if(flag == false){
    
    
                System.out.println("name:"+name+",taste:"+taste);
                flag = true;
                this.notifyAll();
                try {
    
    
                    this.wait();
                } catch (InterruptedException e) {
    
    
                    e.printStackTrace();
                }
            }
        }
    }
}

在这里插入图片描述

这就没有问题了

猜你喜欢

转载自blog.csdn.net/m0_56187876/article/details/115012923