当前位置:网站首页>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();
}
}
边栏推荐
- How to quickly delete the compressed package password?
- 康威定律对于系统架构很重要吗?
- 【HCIE】NO.45 Hub and Spoke配置案例
- 力扣练习——45 二叉树的锯齿形层次遍历
- ZCMU--1891: kotomi and game(C语言)
- 使用GD32F207的高级定时器来产生PWM波出现的隐藏BUG
- 如何运用3DGIS技术整合智慧社区综合管理解决方案
- 【七夕】是时候展现专属于程序员的“浪漫”了
- A practice arrangement about map GIS (below) GIS practice of Redis
- Excel如何解密工作表保护
猜你喜欢
随机推荐
C程序调试过程常见的错误
违约金过高”的认定依据
力扣练习——43 路径总和
关于地图GIS的一次实践整理(下) Redis的GIS实践
Scala basics [common method supplement, pattern matching]
来自雪域高原的馈赠——大凉山高原生态糖心苹果
CubeSat Light-1
如果有些字段不想进行序列化怎么办?
力扣练习——单词搜索
MySQL(7)
递归实现排列型枚举(DAY 93)
300M级mysql数据库跨版本迁移流程
[QNX Hypervisor 2.2用户手册]9.17 tolerance
编译失败:HBuilderX 安装目录不能包括 ( 等特殊字符 (HBuilderX,uni-app报错)
转:张五常:比知识更重要的,是思维方式
[QNX Hypervisor 2.2用户手册]9.18 unsupported
Digicert EV证书签名后出现“证书对于请求用法无效”的解决方案
2022河南萌新联赛第(四)场:郑州轻工业大学 A - ZZULI
跑通CogView教程
STM32 OLED显示屏--SPI通信知识汇总









