当前位置:网站首页>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();
}
}
边栏推荐
猜你喜欢
随机推荐
WiFi、蓝牙、zigbee锁与NB、Cat.1锁的区别
2022河南萌新联赛第(四)场:郑州轻工业大学 A - ZZULI
已更新 联通 电信 tiny模式
The practice of alibaba, data synchronization component canal
UE4 事件图表不小心拉了很远,找不到一开始创建的节点
Line generation 005
UE4 蓝图实现AI随机移动
洛谷P2670扫雷游戏
力扣练习——45 二叉树的锯齿形层次遍历
使用pycharm debug 深度学习代码
浅学一下二叉树的顺序存储结构——堆
力扣练习——38 分割回文串
【Interview】Recruitment requirements
编译失败:HBuilderX 安装目录不能包括 ( 等特殊字符 (HBuilderX,uni-app报错)
UE4 创建暂停和结束游戏UI
2022河南萌新联赛第(四)场:郑州轻工业大学 C - 最大公因数
Crawler_crawl wasde monthly supply and demand balance table (example)
软件测试常见的问题
pg数据库报错问题,有懂的吗
线代005









