JUC编程—生产者与消费者

本文介绍了Java中生产者消费者问题的解决方案,通过synchronized关键字和wait/notify机制解决线程同步问题,并展示了使用Java并发包(JUC)中的Condition接口实现更精确的线程通信。此外,还给出了一个使用Condition实现线程按顺序执行的示例。

生产者与消费者

面试的:单例模式、排序问题、生产者消费者、死锁(需要会手写)

1、synchronzied 版的

package com.zkw.JUC并发编程;

/**
 * 线程通信,生产者与消费者问题: 等待唤醒,通知唤醒
 * 线程交替进行 A  B同时操作一变量 num = 0;
 * A num+1
 * B num-1
 */
public class PC {
    public static void main(String[] args) {
        Data data = new Data();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    data.increment();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"A").start();

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

    }
}

//判断等待,业务,通知
class Data{// 资源类
    private int num = 0;

    // +1
    public synchronized void increment() throws InterruptedException {
       if (num!=0){
           this.wait();
       }
       num++;
       System.out.println(Thread.currentThread().getName()+":"+num);
       this.notifyAll();
    }

    // -1
    public synchronized void decrement() throws InterruptedException {
        if (num==0){
            this.wait();
        }
        num--;
        System.out.println(Thread.currentThread().getName()+":"+num);
        this.notifyAll();
    }
}

问题存在,当存在A、B、C、D就会出现问题,的解决方案

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-mgGWbJ05-1634275430236)在这里插入图片描述

把 if 改成 while 防止虚假唤醒

package com.zkw.JUC并发编程;

/**
 * 线程通信,生产者与消费者问题: 等待唤醒,通知唤醒
 * 线程交替进行 A  B同时操作一变量 num = 0;
 * A num+1
 * B num-1
 */
public class PC {
    public static void main(String[] args) {
        Data data = new Data();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    data.increment();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"A").start();

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

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

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

    }
}

//判断等待,业务,通知
class Data{// 资源类
    private int num = 0;

    // +1
    public synchronized void increment() throws InterruptedException {
      while (num!=0){
           this.wait();
       }
       num++;
       System.out.println(Thread.currentThread().getName()+":"+num);
       this.notifyAll();
    }

    // -1
    public synchronized void decrement() throws InterruptedException {
        while (num==0){
            this.wait();
        }
        num--;
        System.out.println(Thread.currentThread().getName()+":"+num);
        this.notifyAll();
    }
}

2、JUC版的生产者与消费者

在这里插入图片描述

在这里插入图片描述

package com.zkw.JUC并发编程;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class PC2 {
    public static void main(String[] args) {
        Data2 data2 = new Data2();

        new Thread(()->{
            for (int i = 0; i < 5; i++) {
                data2.increment();
            }
        },"A").start();

        new Thread(()->{
            for (int i = 0; i < 5; i++) {
                data2.decrement();
            }
        },"B").start();

        new Thread(()->{
            for (int i = 0; i < 5; i++) {
                data2.increment();
            }
        },"C").start();

        new Thread(()->{
            for (int i = 0; i < 5; i++) {
                data2.decrement();
            }
        },"D").start();

    }
}

//判断等待,业务,通知
class Data2{// 资源类
    private int num = 0;

    Lock lock = new ReentrantLock();
    Condition condition = lock.newCondition();

    // +1
    public  void increment()  {
        lock.lock();
        try {
            while (num!=0){
                condition.await();//线程等待
            }
            num++;
            condition.signalAll();//唤醒所有等待的线程
            System.out.println(Thread.currentThread().getName()+":"+num);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();//解锁
        }
    }

    // -1
    public  void decrement() {
        lock.lock();
        try {
            while (num==0){
                condition.await();
            }
            num--;
            condition.signalAll();
            System.out.println(Thread.currentThread().getName()+":"+num);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}

3、condition能做到精准通知

package com.zkw.JUC并发编程;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
 * 让A、B、C按顺序执行
 */

public class C {
    public static void main(String[] args) {
        Data3 data3 = new Data3();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                data3.printA();
            }
        },"A").start();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                data3.printB();
            }
        },"B").start();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                data3.printC();
            }
        },"C").start();
    }
}

class Data3{ //资源类
    Lock lock = new ReentrantLock();
    Condition condition1 = lock.newCondition();
    Condition condition2 = lock.newCondition();
    Condition condition3 = lock.newCondition();
    private int number = 1;// 1A 2B 3C
    public void printA(){
        lock.lock();
        try {
            //业务,判断 -> 执行 -> 通知
            while (number!=1){
                //等待
                condition1.await();
            }
            System.out.println(Thread.currentThread().getName()+"=>AAAAAA");
            //唤醒,唤醒指定的线程 B
            number = 2;
            condition2.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void printB(){
        lock.lock();
        try {
            //业务,判断 -> 执行 -> 通知
            while (number!=2){
                //等待
                condition2.await();
            }
            System.out.println(Thread.currentThread().getName()+"=>BBBBBBB");
            //唤醒,唤醒指定的线程 C
            number = 3;
            condition3.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void printC(){
        lock.lock();
        try {
            //业务,判断 -> 执行 -> 通知
            while (number!=3){
                //等待
                condition3.await();
            }
            System.out.println(Thread.currentThread().getName()+"=>CCCCCCCCC");
            //唤醒,唤醒指定的线程 A
            number = 1;
            condition1.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值