多线程通信问题

多线程通信问题

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();
                }
            }
        }
    }
}

在这里插入图片描述

这就没有问题了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Fun灬小鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值