【孔乙已】生产者消费者有四样写法

Lock中的Condition可以实现上面Objectwait,notify一样的效果
await对应wait,signal对应notify,signalAll对应notifyAll
下面直接来看看实现,代码与使用wait,notify基本上是一样的,只是同步方式不同

public class ProductorConsumerDemo2 {

private static ReentrantLock lock = new ReentrantLock();
private static Condition full = lock.newCondition();
private static Condition empty = lock.newCondition();

public static void main(String[] args) {
LinkedList linkedList = new LinkedList();
ExecutorService service = Executors.newFixedThreadPool(15);
for (int i = 0; i < 5; i++) {
service.submit(new Productor(linkedList, 8, lock));
}
for (int i = 0; i < 10; i++) {
service.submit(new Consumer(linkedList, lock));
}

}

static class Productor implements Runnable {

private List list;
private int maxLength;
private Lock lock;

public Productor(List list, int maxLength, Lock lock) {
this.list = list;
this.maxLength = maxLength;
this.lock = lock;
}

@Override
public void run() {
while (true) {
lock.lock();
try {
while (list.size() == maxLength) {
System.out.println(“生产者” + Thread.currentThread().getName() + " list以达到最大容量,进行wait");
full.await();
System.out.println(“生产者” + Thread.currentThread().getName() + " 退出wait");
}
Random random = new Random();
int i = random.nextInt();
System.out.println(“生产者” + Thread.currentThread().getName() + " 生产数据" + i);
list.add(i);
empty.signalAll();
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
}

static class Consumer implements Runnable {

private List list;
private Lock lock;

public Consumer(List list, Lock lock) {
this.list = list;
this.lock = lock;
}

@Override
public void run() {
while (true) {
lock.lock();
try {
while (list.isEmpty()) {
System.out.println(“消费者” + Thread.currentThread().getName() + " list为空,进行wait");
empty.await();
System.out.println(“消费者” + Thread.currentThread().getName() + " 退出wait");
}
Integer element = list.remove(0);
System.out.println(“消费者” + Thread.currentThread().getName() + " 消费数据:" + element);
full.signalAll();
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
}

}

3.BlockingQueue方式实现

由于BlockingQueue内部实现就附加了两个阻塞操作。
即当队列已满时,阻塞向队列中插入数据的线程,直至队列中未满;当队列为空时,阻塞从队列中获取数据的线程,直至队列非空时为止.
所以使用BlockingQueue来实现生产者消费者模式非常简单方便,关于BlockingQueue的更多细节可见:并发容器之BlockingQueue详解

下面直接看下生产者消费者实现

public class ProductorConsumerDmoe3 {

private static LinkedBlockingQueue queue = new LinkedBlockingQueue<>();

public static void main(String[] args) {
ExecutorService service = Executors.newFixedThreadPool(15);
for (int i = 0; i < 5; i++) {
service.submit(new Productor(queue));
}
for (int i = 0; i < 10; i++) {
service.submit(new Consumer(queue));
}
}

static class Productor implements Runnable {
private BlockingQueue queue;

public Productor(BlockingQueue queue) {
this.queue = queue;
}

@Override
public void run() {
try {
while (true) {
Random random = new Random();
int i = random.nextInt();
System.out.println(“生产者” + Thread.currentThread().getName() + “生产数据” + i);
queue.put(i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

static class Consumer implements Runnable {
private BlockingQueue queue;

public Consumer(BlockingQueue queue) {
this.queue = queue;
}

@Override
public void run() {
try {
while (true) {
Integer element = (Integer) queue.take();
System.out.println(“消费者” + Thread.currentThread().getName() + “正在消费数据” + element);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

}

4.Kotlin Channel方式实现

随着Kotlin的普及,使用协程来处理并发也变成了一个更加方便的选择
使用Kotlin Channel同样可以实现生产者消费者模式

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级安卓工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Android移动开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Android)
img

文末

我总结了一些Android核心知识点,以及一些最新的大厂面试题、知识脑图和视频资料解析。

需要的小伙伴私信【学习】我免费分享给你,以后的路也希望我们能一起走下去。(谢谢大家一直以来的支持,需要的自己领取)

直接点击链接也可以领取哦!

Android学习PDF+架构视频+面试文档+源码笔记

部分资料一览:

  • 330页PDF Android学习核心笔记(内含8大板块)

  • Android学习的系统对应视频

  • Android进阶的系统对应学习资料

  • Android BAT大厂面试题(有解析)

存中…(img-gQ8UFds2-1710677726184)]

[外链图片转存中…(img-9qnJLuTg-1710677726185)]

  • Android学习的系统对应视频

  • Android进阶的系统对应学习资料

[外链图片转存中…(img-pNmOT5HN-1710677726185)]

  • Android BAT大厂面试题(有解析)

  • 19
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值