博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
多线程之生产者消费者
阅读量:5941 次
发布时间:2019-06-19

本文共 3295 字,大约阅读时间需要 10 分钟。

hot3.png

也是一个笔试题,诸多借口没做好。还是自己没理解透多线程。吾日三省吾身。
代码很多copy JDK6.0的API,也有改写一点点。
class Product {}/** *该类是JDK6.0API中的Condition中的示例。 *此外现成的BlockingQueue的实现:用于生产者-使用者队列,而且是线程安全的。 *BlockingQueue的实现的所有排队方法都可以使用内部锁或其他形式的并发控制来自动达到它们的目的。 */class Storage
{ private int capacity; private int count; private T[] items; int putptr, takeptr; private Lock lock = new ReentrantLock(); private Condition notFull = lock.newCondition(); private Condition notEmpty = lock.newCondition(); public Storage(int capacity,Class
type) { this.capacity=capacity; items=(T[])Array.newInstance(type, capacity); } public void put(T x) throws InterruptedException { lock.lock(); try { while (count == capacity) notFull.await(); items[putptr] = x; if (++putptr == capacity) putptr = 0; ++count; notEmpty.signal(); } finally { lock.unlock(); } } public T take() throws InterruptedException { lock.lock(); try { while (count == 0) notEmpty.await(); T x = items[takeptr]; if (++takeptr == capacity) takeptr = 0; --count; notFull.signal(); return x; } finally { lock.unlock(); } }}/** * 生产者、消费者来自JDK6.0 API BlockingQueue里的示例 */class Producer implements Runnable { public void run() { while(!Thread.currentThread().isInterrupted()) { try{ cLock.lockInterruptibly(); try{ if(count>32) { Thread.currentThread().interrupt(); System.out.println(name+":退出"); } storage.put(produce()); }finally{cLock.unlock();} }catch (InterruptedException ex){Thread.currentThread().interrupt(); } } } private final Storage
storage; private String name; private static int count; private static Lock cLock=new ReentrantLock() ; public Producer(Storage
storage,String name) { this.storage=storage; this.name=name; } private Product produce() throws InterruptedException { Thread.sleep(600); cLock.lockInterruptibly(); try{ count++; System.out.println(name+"生产第"+count+"个"); return new Product(); }finally{cLock.unlock();} } }class Consumer implements Runnable { public void run() { while(!Thread.currentThread().isInterrupted()) { try{ cLock.lockInterruptibly(); try{ if(count>32) { Thread.currentThread().interrupt(); System.out.println(name+":退出"); } consume(storage.take()); }finally{cLock.unlock();} } catch (InterruptedException ex){Thread.currentThread().interrupt(); } } } private final Storage
storage; private String name; private static int count; private static Lock cLock=new ReentrantLock() ; public Consumer(Storage
storage,String name) { this.storage=storage; this.name=name; } void consume(Product product) throws InterruptedException { Thread.sleep(800); cLock.lockInterruptibly(); try{ count++; System.out.println(name+"消费第"+count+"个"); }finally{cLock.unlock();} } }public class Cs { public static void main(String[] args) { Storage
q = new Storage
(20,Product.class); Producer p = new Producer(q,"p"); Consumer c1 = new Consumer(q,"c1"); Consumer c2 = new Consumer(q,"c2"); new Thread(p).start(); new Thread(c1).start(); new Thread(c2).start(); } }

转载于:https://my.oschina.net/braveCS/blog/548794

你可能感兴趣的文章
Android网络之数据解析----SAX方式解析XML数据
查看>>
Java递归列出所有文件和文件夹
查看>>
[关于SQL]查询成绩都大于80分的学生
查看>>
Delphi(Tuxedo,BDE,ADO)三合一数据集组件HsTxQuery
查看>>
java之ibatis数据缓存
查看>>
“TNS-03505:无法解析名称”问题解决一例
查看>>
LeetCode - Longest Common Prefix
查看>>
Android图片处理
查看>>
2015年第21本:万万没想到,用理工科思维理解世界
查看>>
大家谈谈公司里的项目经理角色及职责都是干什么的?
查看>>
剑指offer
查看>>
Velocity魔法堂系列二:VTL语法详解
查看>>
NopCommerce架构分析之八------多语言
查看>>
转:Eclipse自动补全功能轻松设置
查看>>
ES6新特性:Javascript中的Reflect对象
查看>>
hibernate逆向工程生成的实体映射需要修改
查看>>
mysql update操作
查看>>
Robots.txt - 禁止爬虫(转)
查看>>
MySQL数据库
查看>>
项目分析_xxoo-master
查看>>