当前位置:网站首页>07-传统的生产者消费者问题、防止虚假唤醒
07-传统的生产者消费者问题、防止虚假唤醒
2022-08-02 04:53:00 【Casey·Hu】
07-传统的生产者消费者问题、防止虚假唤醒
锁是什么,如何判断锁的是谁?
4、生产者和消费者问题
Synchronized 版本 wait 和 notify可以实现
JUC 版本 lock
生产者和消费者问题 Synchronized 版本
package com.hkx.pc;
/** * @program: juc * @description: A类 * @author: Casey Hu * @create: 2022-07-31 19:05 **/
/** * 线程之间的通讯问题:生产者和消费者问题 * 线程交替执行 A 线程 B线程 操作同一个变量 num=0 * 在A线程执行num加一的操作 * B线程之后执行num减一的操作 * 解决:使用通知唤醒,等待唤醒 */
public class A {
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) {
throw new RuntimeException(e);
}
}
},"A").start();
new Thread(()->{
for (int i=0;i<10;i++){
try {
data.decrement();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
},"B").start();
}
}
/** * @Description: 资源类 * @Param: * @return: * @Author: Casey Hu * @Date: 2022/7/31 */
//判断等待,业务,通知
class Data{
private int number=0;
//加一操作
public synchronized void increment() throws InterruptedException {
if (number!=0){
this.wait(); //等待操作
}
number++;
System.out.println(Thread.currentThread().getName()+"=>"+number);
//通知其他线程,完成了加一操作
this.notifyAll();
}
//减一操作
public synchronized void decrement() throws InterruptedException {
if (number==0){
this.wait();
}
number--;
System.out.println(Thread.currentThread().getName()+"=>"+number);
//通知其他线程,完成了减一操作
this.notifyAll();
}
}
问题存在,A B C D 4个线程 虚假唤醒
要把if 判断改成 while 判断
package com.hkx.pc;
/** * @program: juc * @description: A类 * @author: Casey Hu * @create: 2022-07-31 19:05 **/
/** * 线程之间的通讯问题:生产者和消费者问题 * 线程交替执行 A 线程 B线程 操作同一个变量 num=0 * 在A线程执行num加一的操作 * B线程之后执行num减一的操作 * 解决:使用通知唤醒,等待唤醒 */
public class A {
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) {
throw new RuntimeException(e);
}
}
},"A").start();
new Thread(()->{
for (int i=0;i<10;i++){
try {
data.decrement();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
},"B").start();
new Thread(()->{
for (int i=0;i<10;i++){
try {
data.decrement();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
},"C").start();
new Thread(()->{
for (int i=0;i<10;i++){
try {
data.decrement();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
},"D").start();
}
}
/** * @Description: 资源类 * @Param: * @return: * @Author: Casey Hu * @Date: 2022/7/31 */
//判断等待,业务,通知
class Data{
private int number=0;
//加一操作
public synchronized void increment() throws InterruptedException {
while (number!=0){
this.wait(); //等待操作
}
number++;
System.out.println(Thread.currentThread().getName()+"=>"+number);
//通知其他线程,完成了加一操作
this.notifyAll();
}
//减一操作
public synchronized void decrement() throws InterruptedException {
while (number==0){
this.wait();
}
number--;
System.out.println(Thread.currentThread().getName()+"=>"+number);
//通知其他线程,完成了减一操作
this.notifyAll();
}
}
边栏推荐
猜你喜欢
随机推荐
Redis常见题型
MySQL(7)
软件测试分析流程及输出项包括哪些内容?
数学建模学习笔记:层次分析法(AHP)
C语言特殊运算符
质数路径(DAY 99)
捷信将ESG理念注入企业DNA致力于提供“负责任的消费金融服务”
CodeTON Round 2 (Div. 1 + Div. 2, Rated, Prizes!) C题
使用pycharm debug 深度学习代码
力扣练习——33 原子的数量
matlab simulink 飞机飞行状态控制
UE4 蓝图实现AI随机移动
翻转(DAY 97)
【MLT】MLT多媒体框架生产消费架构解析(一)
STM32 OLED显示屏--SPI通信知识汇总
How to decrypt worksheet protection in Excel
MES如何做好生产过程监控,本文给出了详细解答
【疑问】最终推荐的loose pattern 如果依赖module 没有加载完毕,行为如何,是否报错
【数字IC手撕代码】Verilog固定优先级仲裁器|题目|原理|设计|仿真
迅为RK3568开发板编译Buildroot-全自动编译