Java多种方式实现生产者消费者模式
作者:lkjhgfdsa123 发布时间:2023-12-13 05:56:16
标签:java,生产者,消费者
实现需求:两个线程交替打印1,0,打印10轮
java多线程口诀:
高内聚,低耦合
线程操作资源类
判断干活通知
防止虚假唤醒
方式一:使用synchronized和Object的wait和notifyAll方法
wait:使当前线程阻塞
notify,notifyAll唤醒当前线程
/**
* 两个线程交替打印1,0 打印10轮
*
* @author Administrator
* @version 1.0 2020年7月12日
* @see ProdConsumerDemo1
* @since 1.0
*
*/
class ShareData1 {
public int number = 0;
public synchronized void increment() throws Exception {
while (number != 0) {
this.wait();
}
number++;
System.out.println(Thread.currentThread().getName() + " " + number);
this.notifyAll();
}
public synchronized void decrement() throws InterruptedException {
while (number != 1) {
this.wait();
}
number--;
System.out.println(Thread.currentThread().getName() + " " + number);
this.notifyAll();
}
}
public class ProdConsumerDemo1 {
public static void main(String[] args) {
ShareData1 shareData = new ShareData1();
new Thread(() -> {
for (int i = 0; i < 10; i++) {
try {
shareData.increment();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}, "A").start();
new Thread(() -> {
for (int i = 0; i < 10; i++) {
try {
shareData.decrement();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}, "B").start();
}
}
输出结果
A 1
B 0
A 1
B 0
A 1
B 0
A 1
B 0
A 1
B 0
A 1
B 0
A 1
B 0
A 1
B 0
A 1
B 0
A 1
B 0
方式二:使用jdk1.8的Lock和Condition
class ShareData2 {
private int number = 0;
private Lock lock = new ReentrantLock();
private Condition condition = lock.newCondition();
public void increment() throws Exception {
lock.lock();
try {
while (number != 0) {
condition.await();
}
number++;
System.out.println(Thread.currentThread().getName() + " " + number);
condition.signalAll();
} finally {
lock.unlock();
}
}
public void decrement() throws InterruptedException {
lock.lock();
try {
while (number != 1) {
condition.await();
}
number--;
System.out.println(Thread.currentThread().getName() + " " + number);
condition.signalAll();
} finally {
// TODO: handle finally clause
lock.unlock();
}
}
}
public class ProdConsumerDemo2 {
public static void main(String[] args) {
ShareData2 shareData = new ShareData2();
new Thread(() -> {
for (int i = 0; i < 10; i++) {
try {
shareData.increment();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}, "A").start();
new Thread(() -> {
for (int i = 0; i < 10; i++) {
try {
shareData.decrement();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}, "B").start();
}
}
输出结果
A 1
B 0
A 1
B 0
A 1
B 0
A 1
B 0
A 1
B 0
A 1
B 0
A 1
B 0
A 1
B 0
A 1
B 0
A 1
B 0
主要是熟悉Lock和Condition的使用
Lock和Condition相比于synchronized,能够精确唤醒
需求:三个线程A,B,C顺序打印,A打印5次,B打印10次,C打印15次,10轮
class ShareData3 {
private int number = 1;
private Lock lock = new ReentrantLock();
private Condition c1 = lock.newCondition();
private Condition c2 = lock.newCondition();
private Condition c3 = lock.newCondition();
public void print5() throws Exception {
lock.lock();
try {
while (number != 1) {
c1.await();
}
number = 2;
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + " " + i);
}
c2.signalAll();
} finally {
// TODO: handle finally clause
lock.unlock();
}
}
public void print10() throws InterruptedException {
lock.lock();
try {
while (number != 2) {
c2.await();
}
number=3;
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName() + " " + i);
}
c3.signalAll();
} finally {
// TODO: handle finally clause
lock.unlock();
}
}
public void print15() throws InterruptedException {
lock.lock();
try {
while (number != 3) {
c3.await();
}
number = 1;
for (int i = 0; i < 15; i++) {
System.out.println(Thread.currentThread().getName() + " " + i);
}
c1.signalAll();
} finally {
// TODO: handle finally clause
lock.unlock();
}
}
}
public class ProdConsumerDemo3 {
public static void main(String[] args) {
ShareData3 shareData3 = new ShareData3();
new Thread(() -> {
try {
for (int i = 0; i < 10; i++) {
shareData3.print5();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}, "A").start();
new Thread(() -> {
try {
for (int i = 0; i < 10; i++) {
shareData3.print10();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}, "B").start();
new Thread(() -> {
try {
for (int i = 0; i < 10; i++) {
shareData3.print15();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}, "C").start();
}
}
来源:https://www.cnblogs.com/lt123/p/13290098.html


猜你喜欢
- 这篇文章主要介绍了Java多态中动态绑定原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以
- 前言我们在很大的项目开发,会发现项目引用的 dll 会很多,我想要按照不同的功能,将不同的 dll 放在不同的文件夹简单的方法是通过修改 A
- @Autowired加到接口上但获取的是实现类问题Spring的@Autowired加到接口上但获取的是实现类? &
- 通过java的File类创建临时文件,然后在程序退出时自动删除临时文件。下面将通过创建一个JFrame界面,点击创建按钮在当前目录下面创建t
- 本文为大家分享了Android AIDL实现两个APP间的跨进程通信实例,供大家参考,具体内容如下1 Service端创建首先需要创建一个A
- 1.传递引用在一个方法中将一个对象的引用传递给另外一个方法,引用指向的对象是同一个public class Person {int age;
- 上次给一个网站写网站 前后端分离 最后跪在ajax跨域上面了 自己在网上找了个方法 亲试可用
- 简介OCSP在线证书状态协议是为了替换CRL而提出来的。对于现代web服务器来说一般都是支持OCSP的,OCSP也是现代web服务器的标配。
- 原理简介 & OpenGL 的优势裸眼 3D 效果的本质是——将整个图片结构分为 3 层:上
- 这篇文章主要介绍了Spring Cloud Zuul添加过滤器过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学
- 目录一、C# 多态性二、静态多态性三、函数重载四、C# 运算符重载1、运算符重载的实现2、可重载和不可重载运算符五、动态多态性前言:👻🎄学过
- 1:在 Visual Studio Code 中打开扩展视图(Ctrl+Shift+X),输入关键词java、spring分别下载Java开
- springboot的最强大的就是那些xxxAutoconfiguration,但是这些xxxAutoConfiguration又依赖那些s
- spring boot是个好东西,可以不用容器直接在main方法中启动,而且无需配置文件,方便快速搭建环境。可是当我们要同时启动2个spri
- Android存储方式有很多种,在这里所用的存储方式是SharedPreferrences,其采用了Map数据结构来存储数据,以键值的方式存
- 继上一次利用Servlet实现图片上传,这次利用基于MVC的Struts框架,封装了Servlet并简化了JSP页面跳转。JSP上传页面上传
- 一、简介:介绍两种使用 BitmapTransformation 来实现 Glide 加载圆形图片和圆角图片的方法。Glide 并不能直接支
- 背景事情是酱紫的,阿星的上级leader负责记录信息的业务,每日预估数据量是15万左右,所以引入sharding-jdbc做分表。上级lea
- 东西不多,但一般项目够用了。public class RegularUtil { //身份证 publi
- 本文实例分析了Android中ListView用法。分享给大家供大家参考,具体如下:通过在Layout中添加ListView Widget可