当前位置:网站首页>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();
}
}
边栏推荐
- 斐波那契数列
- 力扣练习——37 复原IP地址
- 讯飞AIUI智能机器人5-----让器理解你(语音技术综合应用)
- 捷信将ESG理念注入企业DNA致力于提供“负责任的消费金融服务”
- P1012 [NOIP1998 提高组] 拼数
- 直播 | 7.30 ApacheCon Asia 2022 IOT/IIOT专题,IoTDB PMC 乔嘉林担任出品人
- 来自雪域高原的馈赠——大凉山高原生态糖心苹果
- CodeTON Round 2 (Div. 1 + Div. 2, Rated, Prizes!)
- Deep Blue Academy - Handwritten VIO Homework - Chapter 2
- 力扣练习——41 对称二叉树
猜你喜欢

【Gazebo入门教程】第一讲 Gazebo的安装、UI界面、SDF文件介绍

Live | 7.30 ApacheCon Asia 2022 IOT/IIOT topic, IoTDB PMC Qiao Jialin as the producer

关于地图GIS开发事项的一次实践整理(上)

【云原生】什么是CI/CD? | CI/CD 带来的好处

Crawler_crawl wasde monthly supply and demand balance table (example)

Towhee 每周模型

PDF文件转换格式

Minecraft 1.18.1、1.18.2模组开发 23.3D动画盔甲制作

来自雪域高原的馈赠——大凉山高原生态糖心苹果

数学建模笔记:TOPSIS方法(优劣解距离法)和熵权法修正
随机推荐
【七夕】是时候展现专属于程序员的“浪漫”了
分享|5G+智慧工业园区解决方案(附PDF)
MES系统物料管理的五大功能,建议收藏
合作的小伙伴,缺乏主人翁(owner)意识,好苦恼
直播 | 7.30 ApacheCon Asia 2022 IOT/IIOT专题,IoTDB PMC 乔嘉林担任出品人
从DES走到AES(现代密码的传奇之路)
【STM32】 ADC模数转换
UE4 AI行为树实现随机和跟随移动
【面试】招聘要求
Excel如何解密工作表保护
力扣练习——39 正方形数组的数目
迅为RK3568开发板编译Buildroot-全自动编译
洛谷P2670扫雷游戏
How to quickly delete the compressed package password?
软件测试分析流程及输出项包括哪些内容?
IOT物联网概述及应用层架构入门篇
【HCIE】NO.30 OSPFv3的基本配置
力扣练习——48 找到小镇的法官
系统层面知识连接收藏
C程序调试过程常见的错误