当前位置:网站首页>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();
}
}
边栏推荐
猜你喜欢
随机推荐
Scala basics [common method supplement, pattern matching]
翻转(DAY 97)
How to quickly delete the compressed package password?
Centos7.9+mysql8.0开启指定IP远程连接数据库
2022河南萌新联赛第(四)场:郑州轻工业大学 C - 最大公因数
MES系统物料管理的五大功能,建议收藏
应用pca和K-means实现用户对物品类别的喜好细分划分
力扣练习——37 复原IP地址
递归实现排列型枚举(DAY 93)
Liquidated damages are too high"
区间和 离散化
【Interview】Recruitment requirements
A Practical Arrangement of Map GIS Development Matters (Part 1)
系统层面知识连接收藏
力扣练习——48 找到小镇的法官
UE4 创建暂停和结束游戏UI
gergovia's deal tijie
Excel如何解密工作表保护
C语言:结构体总结
Towhee 每周模型








