java实现<生产者-消费者>的两种方式

本文介绍了两种实现生产者-消费者模式的方法:使用synchronized关键字和Lock接口。通过具体代码示例,展示了如何利用wait()、notifyAll()及Condition的await()、signalAll()等方法来确保线程间的正确同步。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1,两种方式简单介绍:

1.1 使用Synchronized关键字

Synchronized + wait() + notifyAll()

注意点:

判断条件用while循环,如果用if会出现虚假唤醒。

1.2使用Lock接口

ReentrantLock + Condition + await() + singsignalAll()

注意点:

Lock lock = new ReentrantLock(); //注意Lock 的实现类
Condition condition = lock.newCondition(); //注意Condition通过lock获取

2,使用synchronized

直接上代码:

/**
 * 资源类
 * @author dangkaimin
 */
public class Source {
    private int number = 0; //资源
    /* 生产 */
    public synchronized void push() {
        while(number == 1){  //判断 注意判断条件用while,如果用if会出现虚假唤醒
            this.wait();     //等待
        }	
        number = 1;			 //业务
        System.out.println(Thread.currentThread().getName() + "push number = " + number);
        this.notifyAll();    //通知
    }
    /* 消费 */
    public synchronized void pop() {
        while(number == 0){     //判断
            this.wait();        //等待
        }
        number = 0;				//业务
        System.out.println(Thread.currentThread().getName() + "pop number = " + number);
        this.notifyAll();       //通知
    }

    public static void main(String[] args){
        Source source = new Source();
        /* 生产者*/
        new Thread( () -> {
            while(true) {
                source.push();
            }
        }).start();

        /* 消费者*/
        new Thread( () -> {
            while(true) {
                source.pop();
            }
        }).start();
    }

3,使用Lock接口

/**
 * 资源类
 * @author dangkaimin
 */
public class Source {
    private int number = 0; //资源
    private Lock lock = new ReentrantLock();
    Condition condition = lock.newCondition();

    /* 生产 */
    public void push() {
        lock.lock();
        try {
            while(number == 1){ 	//判断
	            condition.await();	//等待
            }
            number = 1;				//业务
            System.out.println(Thread.currentThread().getName() + " push number = " + number);
            condition.signalAll();   //通知
        }finally {
            lock.unlock();
        }
    }
    /* 消费 */
    public void pop() {
        lock.lock();
        try {
            while(number == 0){		//判断
                condition.await();	//等待
            }
            number = 0;				//业务
            System.out.println(Thread.currentThread().getName() + " pop number = " + number);
            condition.signalAll();   //通知
        } finally {
            lock.unlock();
        }
    }

    public static void main(String[] args){
        Source source = new Source();
        /* 生产者*/
        new Thread( () -> {
            while(true) {
                source.push();
            }
        },"A").start();

        /* 消费者*/
        new Thread( () -> {
            while(true) {
                source.pop();
            }
        },"B").start();
    }
}

3,Lock升级版:通过condition实现线程的精准通知。

流程:A唤醒B,B唤醒C,C唤醒A

注意点:

  • 使用一个锁的不同condition
  • 注意判断条件的合理。
/**
 * 资源类
 * @author dangkaimin
 */
public class Source {
    private int number = 1; //资源
    private Lock lock = new ReentrantLock();
    Condition condition_A = lock.newCondition();
    Condition condition_B = lock.newCondition();
    Condition condition_C = lock.newCondition();

    public void method_A() {
        lock.lock();
        try {
            while(number != 1){  //判断
                condition_A.await();   //等待
            }
            number = 2;
            System.out.println(Thread.currentThread().getName() + " A number = " + number);
            condition_B.signal();   //唤醒A
        }catch(Exception e){

        }finally {
            lock.unlock();
        }
    }
    /* 消费 */
    public void method_B() {
        lock.lock();
        try {
            while(number != 2){     //判断
                condition_B.await();   //等待
            }
            number = 3;
            System.out.println(Thread.currentThread().getName() + " B number = " + number);
            condition_C.signal();   //唤醒B
        }catch(Exception e){

        } finally {
            lock.unlock();
        }
    }
    public void method_C() {
        lock.lock();
        try {
            while(number != 3){     //判断
                condition_C.await();   //等待
            }
            number = 1;
            System.out.println(Thread.currentThread().getName() + " C number = " + number);
            condition_A.signal();   //唤醒A
        }catch(Exception e){

        } finally {
            lock.unlock();
        }
    }

    public static void main(String[] args){
        Source source = new Source();
        new Thread( () -> {
            while(true) {
                source.method_A();
            }
        },"method_A").start();

        new Thread( () -> {
            while(true) {
                source.method_B();
            }
        },"method_B").start();
        new Thread( () -> {
            while(true) {
                source.method_C();
            }
        },"method_C").start();
    }
}

精准通知结果如下:

method_C C number = 1
method_A A number = 2
method_B B number = 3
method_C C number = 1
method_A A number = 2
method_B B number = 3
method_C C number = 1
method_A A number = 2
method_B B number = 3
...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

大豆芽菜儿

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

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

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

打赏作者

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

抵扣说明:

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

余额充值