JUC(生产者消费者模型、Lock&Condition)

目录

1. 信号灯法(Synchronized实现)

解决虚假唤醒​编辑

2. 信号灯法(Lock实现)

2.1 基本实现

2.2 Condition精准通知

3 管程法


 

1. 信号灯法(Synchronized实现)

public class PCDemo {
    public static void main(String[] args) {
        Data data = new Data();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    data.produce();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"A").start();

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

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

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

    }
}

//判断等待、业务、通知
class Data {
    private int number = 0;

    //+1
    public synchronized void produce() throws InterruptedException {
        //1.判断等待
        while (number != 0) {  // !!!!!!注意解决虚假唤醒,必须用while,不能用if
            wait();
        }
        //2.业务
        number++;
        System.out.println(Thread.currentThread().getName() + "=>" + number);
        //3.通知
        this.notifyAll();
    }

    //-1
    public synchronized void consum() throws InterruptedException {
        //1.判断等待
        while (number == 0) {  // !!!!!!注意解决虚假唤醒,必须用while,不能用if
            wait();
        }
        //2.业务
        number--;
        System.out.println(Thread.currentThread().getName() + "=>" + number);
        //3.通知
        this.notifyAll();
    }
}

解决虚假唤醒

2. 信号灯法(Lock实现)

2.1 基本实现

 

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

public class Data2 {
    private int number = 0;
    private Lock lock = new ReentrantLock();
    private Condition condition = lock.newCondition();

    //+1
    public void produce() throws InterruptedException {
        lock.lock();
        try {
            //1.判断等待
            while (number != 0) {// !!!!!!注意解决虚假唤醒,必须用while,不能用if
                condition.await();
            }
            //2.业务
            number++;
            System.out.println(Thread.currentThread().getName() + "=>" + number);
            //3.通知
            condition.signalAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }

    }

    //-1
    public void consum() throws InterruptedException {
        lock.lock();
        try {
            //1.判断等待
            while (number == 0) {
                condition.await();
            }
            //2.业务
            number--;
            System.out.println(Thread.currentThread().getName() + "=>" + number);
            //3.通知
            condition.signalAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}

2.2 Condition精准通知


public class PCDemo3 {
    public static void main(String[] args) {
        Data3 data = new Data3();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    data.produceA();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "A").start();

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

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

        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    data.consumD();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "D").start();
    }
}
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Data3 {
    private int number = 1;
    private Lock lock = new ReentrantLock();
    private Condition condition1 = lock.newCondition();
    private Condition condition2 = lock.newCondition();
    private Condition condition3 = lock.newCondition();
    private Condition condition4 = lock.newCondition();
    //执行顺序:A->B->C->D

    //+1
    public void produceA() throws InterruptedException {
        lock.lock();
        try {
            //1.判断等待
            while (number != 1) {// !!!!!!注意解决虚假唤醒,必须用while,不能用if
                condition1.await();
            }
            //2.业务
            System.out.println(Thread.currentThread().getName() + "=>" + number);
            number = 2;
            //3.通知
            condition2.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }


    //-1
    public void consumB() throws InterruptedException {
        lock.lock();
        try {
            //1.判断等待
            while (number != 2) {
                condition2.await();
            }
            //2.业务
            System.out.println(Thread.currentThread().getName() + "=>" + number);
            number = 3;
            //3.通知
            condition3.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }


    //+1
    public void produceC() throws InterruptedException {
        lock.lock();
        try {
            //1.判断等待
            while (number != 3) {// !!!!!!注意解决虚假唤醒,必须用while,不能用if
                condition3.await();
            }
            //2.业务
            System.out.println(Thread.currentThread().getName() + "=>" + number);
            number = 4;
            //3.通知
            condition4.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }


    //-1
    public void consumD() throws InterruptedException {
        lock.lock();
        try {
            //1.判断等待
            while (number != 4) {
                condition4.await();
            }
            //2.业务
            System.out.println(Thread.currentThread().getName() + "=>" + number);
            number = 1;
            //3.通知
            condition1.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}

3 管程法

 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值