当前位置:网站首页>Producer and Consumer Problems in Concurrent Programming
Producer and Consumer Problems in Concurrent Programming
2022-08-04 09:03:00 【51CTO】
简单的线程通信,一个线程对数字进行增加操作、另一个线程对线程进行减少操作.
简单解释:线程A对数字进行减少操作,但不会一直让这个数字减少下去.当减少到设定的条件,让其等待,通知其他线程获得该资源.
package com.ProductAndCustomer;
/**
* 线程之间的通信,生产者和消费者问题
*/
public class Product {
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) {
e.printStackTrace();
}
}
},"A").start();
new Thread(()->{
for (int i = 0; i < 10; i++) {
try {
data.decrement();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"B").start();
}
}
//等待,业务,通知
class Data{
//资源类
private int num = 0;
//增加操作
public synchronized void increment() throws InterruptedException {
if(num != 0){
//等待
this.wait();
}
num ++;
System.out.println(Thread.currentThread().getName()+"=>"+num);
//通知其他线程,+1结束
this.notifyAll();
}
//减少操作
public synchronized void decrement() throws InterruptedException {
if(num == 0){
//等待
this.wait();
}
num --;
System.out.println(Thread.currentThread().getName()+"=>"+num);
//通知其他线程,-1结束
this.notifyAll();
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
测试
在多几个线程
package com.ProductAndCustomer;
/**
* 线程之间的通信,生产者和消费者问题
*/
public class Product {
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) {
e.printStackTrace();
}
}
},"A").start();
new Thread(()->{
for (int i = 0; i < 10; i++) {
try {
data.decrement();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"B").start();
new Thread(()->{
for (int i = 0; i < 10; i++) {
try {
data.decrement();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"C").start();
new Thread(()->{
for (int i = 0; i < 10; i++) {
try {
data.decrement();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"D").start();
}
}
//等待,业务,通知
class Data{
//资源类
private int num = 0;
//增加操作
public synchronized void increment() throws InterruptedException {
if(num != 0){
//等待
this.wait();
}
num ++;
System.out.println(Thread.currentThread().getName()+"=>"+num);
//通知其他线程,+1结束
this.notifyAll();
}
//减少操作
public synchronized void decrement() throws InterruptedException {
if(num == 0){
//等待
this.wait();
}
num --;
System.out.println(Thread.currentThread().getName()+"=>"+num);
//通知其他线程,-1结束
this.notifyAll();
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
测试结果
这里会出现虚假唤醒
查看开发文档可知、需要修改判断语句 if修改为while
修改后的代码
package com.ProductAndCustomer;
/**
* 线程之间的通信,生产者和消费者问题
*/
public class Product {
public static void main(String[] args) {
Data data = new Data();
new Thread(()->{
for (int i = 0; i < 5; i++) {
try {
data.increment();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"A").start();
new Thread(()->{
for (int i = 0; i < 5; i++) {
try {
data.decrement();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"B").start();
new Thread(()->{
for (int i = 0; i < 5; i++) {
try {
data.increment();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"C").start();
new Thread(()->{
for (int i = 0; i < 5; i++) {
try {
data.decrement();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"D").start();
}
}
//等待,业务,通知
class Data{
//资源类
private int num = 0;
//增加操作
public synchronized void increment() throws InterruptedException {
while(num != 0){
//等待
this.wait();
}
num ++;
System.out.println(Thread.currentThread().getName()+"=>"+num);
//通知其他线程,+1结束
this.notifyAll();
}
//减少操作
public synchronized void decrement() throws InterruptedException {
while(num == 0){
//等待
this.wait();
}
num --;
System.out.println(Thread.currentThread().getName()+"=>"+num);
//通知其他线程,-1结束
this.notifyAll();
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
测试结果
边栏推荐
- inject() can only be used inside setup() or functional components.
- 抬升市场投资情绪,若羽臣是否还需“自身硬”?
- oracle sql 多表查询
- 蜜芽CEO刘楠:垂直电商黄金时代已落幕 坚定转型品牌之路
- 【论文笔记】Delving into the Estimation Shift of Batch Normalization in a Network
- tcp连接的细节
- MATLAB绘图总结
- Oracle怎么获取当前库或者同一台服务器上某几个库的数据总行数?
- 我和 TiDB 的故事 | 缘份在,那就终是能相遇的
- 智汇华云 | 华云软件定义网络 DCI介绍
猜你喜欢
随机推荐
How many assertion methods are commonly used in JMeter?
Unity3D 数据加密
Could you please talk about how the website is accessed?[Interview questions in the web field]
cannot import name 'import_string' from 'werkzeug' [bug solution]
[Punctuality Atom STM32 Serial] Chapter 4 STM32 First Experience Excerpted from [Punctual Atom] MiniPro STM32H750 Development Guide_V1.1
基于cRIO-904X搭建Simulink与Labview环境
[Computer recording screen] How to use bandicam to record the game setting graphic tutorial
DeLighT:深度和轻量化的Transformer
Anton Paar安东帕密度计比重计维修DMA35性能参数
Post-94 Byte P7 posted the salary slip: It's really good to make up for this...
Detailed explanation of telnet remote login aaa mode [Huawei eNSP]
命里有时终须有--记与TiDB的一次次擦肩而过
张朝阳对话俞敏洪:谈宇宙、谈焦虑、谈创业、谈退休、谈人生
交换机链路聚合详解【华为eNSP】
NAT/NAPT地址转换(内外网通信)技术详解【华为eNSP】
软件工程国考总结——判断题
ZbxTable 2.0 重磅发布!6大主要优化功能!
发现WRH几个表被锁了,怎么办?
【论文笔记】Understanding Long Programming Languages with Structure-Aware Sparse Attention
有坦荡的远方