多线程面试必备-------手写生产者消费者(包括注意点)

生产者消费者(Synchronized版本)注意点:

  1. 资源类中的方法中, 出现的条件判断, 不能用if, 要用while, 可以参考jdk1.8文档object中wait那部分的说明
  2. 打印语句要写在循环外面, 不要写在循环里面,
package com.sonia.productAndConsume;

import com.sun.org.apache.xerces.internal.parsers.CachingParserPool;

//传统的生产者和消费者问题
public class pcDemo01 {
    public static void main(String[] args) {
        Resource resource = new Resource();
            new Thread(()->{
                for (int i = 0; i < 10; i++) {
                    try {
                        resource.product();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            },"A").start();

            new Thread(()->{
                for (int i = 0; i <10 ; i++) {
                    try {
                        resource.consume();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            },"B").start();

        new Thread(()->{
            for (int i = 0; i <10 ; i++) {
                try {
                    resource.product();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"C").start();

        new Thread(()->{
            for (int i = 0; i <10 ; i++) {
                try {
                    resource.consume();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"D").start();
        }



}


//资源类记忆理解口诀, 等待->业务->唤醒

class Resource {
    private int number = 0;

    //生产
    public synchronized void  product() throws InterruptedException {
        while (number != 0) {
            //等待
            this.wait();
        }
        number++;
        System.out.println("线程"+Thread.currentThread().getName()+"生产"+number+"个");
        this.notifyAll();
    }

    public  synchronized void consume() throws InterruptedException {

        //while要替换成if 避免发生虚假唤醒
        while (number == 0) {
            //等待
            this.wait();
            //不是在这里打印的!!!
            //System.out.println("线程"+Thread.currentThread().getName()+"======>消费"+number+"个");
        }
        number--;
        System.out.println("线程"+Thread.currentThread().getName()+"======>消费"+number+"个");
        this.notifyAll();
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值