- 浏览: 463147 次
- 性别:
- 来自: 杭州
-
文章分类
- 全部博客 (146)
- Maven (3)
- Quartz (10)
- Hessian (3)
- JDK (42)
- eclipse (4)
- 设计思想 (6)
- XML (8)
- JavaMail (1)
- Spring (11)
- mina (1)
- HsqlDb (1)
- Cache (2)
- Tool (6)
- 心情 (5)
- JQuery (0)
- Hadoop (5)
- Hbase (3)
- 自动构建 (7)
- JNDI (0)
- 代码赏析 (5)
- Oracle (1)
- Excel (4)
- Effective Java (5)
- JAXB (4)
- fdafasdf (1)
- ccc (0)
- web (3)
- concurrent (1)
- CVS (1)
- eclipse plugin (2)
- Apache (10)
最新评论
-
chxiaowu:
nice!
Quartz实现固定执行次数 -
zxjlwt:
学习了。http://surenpi.com
自定义ClassLoader -
kadlly:
public static final Logger log ...
Hessian 权限认证 -
spring_springmvc:
java程序语言学习教程 地址http://www.zuida ...
Java-Final -
liushuiwuyan:
[img][/img]
设计模式-单例
需求: 多个生产者不断的生产产品,多个消费者不断的消费产品,仓库可以存放10个产品。 第一批产品需要生产20个产品,并消费完毕。
其实使用wait/notify模式实现差不多,只是使用的时候要注意防止“死锁”。
blockingQueue的实现与notify的实现效率差不多.
实现的逻辑少了,这里需要注意的是,queue.take(),如果queue里再也不会有数据,将会”死锁“,防止多线程同时take最后一个,这里加了把锁。
其实使用wait/notify模式实现差不多,只是使用的时候要注意防止“死锁”。
blockingQueue的实现与notify的实现效率差不多.
package ycl.learn.effective.java.thread.pc; import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.atomic.AtomicInteger; public class PublicResourceBlockingQueue { private BlockingQueue<Product> queue = new LinkedBlockingQueue<Product>(10); private AtomicInteger index = new AtomicInteger(1); public static final int MAX_RESOURCE = 20; public boolean increace() { try { if (isComplete()) {// 生产任务完成 return false; } int tindex = index.getAndIncrement(); Product p = new Product(tindex); queue.put(p); System.out.println(Thread.currentThread() + "increace:" + tindex); } catch (InterruptedException e) { e.printStackTrace(); } return true; } public boolean decreace() { try { if (isComplete() && isEmpty()) { return false; } synchronized(this){ if(!isEmpty()){ Product p = queue.take(); System.out.println(Thread.currentThread() + "decreace:" + p.index); } } } catch (InterruptedException e) { e.printStackTrace(); } return true; } /** * 判断是否空了 */ public boolean isEmpty() { return queue.isEmpty(); } /** * 判断生产完毕 */ public boolean isComplete() { return index.get() > MAX_RESOURCE; } class Product { public int index; public Product(int index) { this.index = index; } } }
public static void anotherone() throws InterruptedException{ long start = System.currentTimeMillis(); PublicResourceBlockingQueue ps = new PublicResourceBlockingQueue(); CustomerB c = new CustomerB(ps); ProductorB p = new ProductorB(ps); Thread ct = new Thread(c,"customer0"); Thread ct1 = new Thread(c,"customer1"); Thread ct2 = new Thread(c,"customer2"); Thread ct3 = new Thread(c,"customer3"); Thread ct4 = new Thread(c,"customer4"); Thread ct5 = new Thread(c,"customer5"); Thread ct6 = new Thread(c,"customer6"); Thread pt = new Thread(p,"productor0"); Thread pt1 = new Thread(p,"productor1"); Thread pt2 = new Thread(p,"productor2"); Thread pt3 = new Thread(p,"productor3"); Thread pt4 = new Thread(p,"productor4"); Thread pt5 = new Thread(p,"productor5"); Thread pt6 = new Thread(p,"productor6"); ct.start(); ct1.start(); ct2.start(); ct3.start(); ct4.start(); ct5.start(); ct6.start(); pt.start(); pt1.start(); pt2.start(); pt3.start(); pt4.start(); pt5.start(); pt6.start(); ct.join(); ct1.join(); ct2.join(); ct3.join(); ct4.join(); ct5.join(); ct6.join(); pt.join(); pt1.join(); pt2.join(); pt3.join(); pt4.join(); pt5.join(); pt6.join(); System.out.println(System.currentTimeMillis()-start);//5531 }
Thread[productor5,5,main]increace:5 Thread[productor2,5,main]increace:3 Thread[productor3,5,main]increace:4 Thread[productor6,5,main]increace:7 Thread[productor4,5,main]increace:6 Thread[productor1,5,main]increace:2 Thread[productor0,5,main]increace:1 Thread[productor1,5,main]increace:8 Thread[productor4,5,main]increace:9 Thread[customer1,5,main]decreace:6 Thread[productor0,5,main]increace:10 Thread[customer4,5,main]decreace:5 Thread[productor4,5,main]increace:11 Thread[productor6,5,main]increace:12 Thread[customer1,5,main]decreace:7 Thread[productor0,5,main]increace:13 Thread[customer2,5,main]decreace:4 Thread[productor3,5,main]increace:14 Thread[customer5,5,main]decreace:3 Thread[productor5,5,main]increace:15 Thread[customer4,5,main]decreace:2 Thread[productor0,5,main]increace:16 Thread[customer3,5,main]decreace:1 Thread[productor2,5,main]increace:17 Thread[customer6,5,main]decreace:8 Thread[customer0,5,main]decreace:9 Thread[customer3,5,main]decreace:10 Thread[customer5,5,main]decreace:11 Thread[productor1,5,main]increace:18 Thread[customer3,5,main]decreace:12 Thread[productor4,5,main]increace:19 Thread[productor5,5,main]increace:20 Thread[customer2,5,main]decreace:13 Thread[customer5,5,main]decreace:14 Thread[productor0,5,main] exist Thread[customer1,5,main]decreace:15 Thread[productor5,5,main] exist Thread[productor1,5,main] exist Thread[customer4,5,main]decreace:16 Thread[customer2,5,main]decreace:17 Thread[productor3,5,main] exist Thread[customer6,5,main]decreace:18 Thread[customer4,5,main]decreace:19 Thread[customer1,5,main]decreace:20 Thread[productor6,5,main] exist Thread[customer5,5,main] exist Thread[customer3,5,main] exist Thread[productor2,5,main] exist Thread[customer0,5,main] exist Thread[productor4,5,main] exist Thread[customer6,5,main] exist Thread[customer2,5,main] exist Thread[customer1,5,main] exist Thread[customer4,5,main] exist 1234
实现的逻辑少了,这里需要注意的是,queue.take(),如果queue里再也不会有数据,将会”死锁“,防止多线程同时take最后一个,这里加了把锁。
发表评论
-
Java Application Cache
2016-09-27 19:25 838Application Cache is used very ... -
Java 字符串分词
2015-01-02 14:43 1690在Java的世界里有个类型 ... -
jdk 1.6 新特性,集成Groovy, 性能很差
2014-04-02 14:27 1233性能都是相对的,如果调用量不是很大的话,可以忽略,毕竟使用为主 ... -
Fake Code easy implements
2014-04-01 15:41 994package org.miniframe.modules ... -
JDK regex 用法及用途
2014-03-31 15:48 1177查找 Boolean flag = pattern.mat ... -
生产者消费者(四)
2014-03-04 12:32 1112需求: 多个生产者不断的生产产品,多个消费者不断的消费产品,仓 ... -
生产者消费者(三)
2014-03-04 10:59 927需求: 多个生产者不断的生产产品,多个消费者不断的消费产品,仓 ... -
生产者消费者模式(一)
2014-02-28 14:30 999需求: 多个生产者不断的生产产品,多个消费者不断的消费产品,仓 ... -
查看Class文件使用的JDK版本
2013-10-30 14:17 1080由于JDK一般是向下兼容的,所以有时候本地的JDK版本比类库的 ... -
Java源代码转码
2012-12-20 17:22 1288现在中国的项目很多,编码无非是UTF-8,GBK,GB2312 ... -
Tomcat集成OSGI,并通过JNDI开放Web调用
2012-12-03 11:22 3089Tomcat集成OSGi,首先要选择OSGI服务器,我这里采用 ... -
JDK的Logging
2012-11-07 15:49 1654jdk自带有一个log日志,对于一般的使用,仅够了. 代码如下 ... -
java.util.*
2012-11-06 14:23 1341java.util 工具包,灰常的有用,有机会一定要研读源码。 ... -
java.util.concurrent.*
2012-11-02 10:38 17391. java.util.concurrent.ArrayBl ... -
java.util.rt.*
2012-10-31 13:51 10791. java.util.HashMap 散列表,主要是以离散 ... -
巧秒设计方法,不返回null
2016-09-27 19:32 661/** * {@inheritDoc} * ... -
java doc 代码文档
2012-07-13 13:58 1287对于代码规范不解释了,网上很多。 在编写代码的时候,有一点灰 ... -
接口与抽象类
2012-07-11 16:53 10931. 接口设计必谨慎,除非业务变更,否则打死不能动接口。[不变 ... -
JVM优化机制好诡异
2012-04-20 08:43 1430long i[] = new long[1000000]; ... -
JVM优化机制好诡异
2016-09-27 19:32 534long i[] = new long[100000 ...
相关推荐
生产者消费者问题解决方案 生产者消费者问题是计算机科学中的一种经典问题,描述的是在多线程环境中,多个生产者线程和消费者线程之间的协作问题。生产者线程负责生产数据,并将其存储在缓冲区中,而消费者线程则从...
多生产者,多消费者问题源代码多生产者,多消费者问题源代码多生产者,多消费者问题源代码多生产者,多消费者问题源代码多生产者,多消费者问题源代码多生产者,多消费者问题源代码多生产者,多消费者问题源代码多...
生产者消费者流程图; 生产者消费者流程图。
生产者消费者问题总结 信号量概念总结 经典生产者-消费者问题 较为复杂的生产者-消费者问题 华南理工大学生产者和消费者问题 个人总结
操作系统实验二:生产者——消费者问题 1. 在Windows操作系统上,利用Win32 API提供的信号量机制,编写应用程序实现生产者——消费者问题。 2. 在Linux操作系统上,利用Pthread API提供的信号量机制,编写应用程序...
生产者消费者的实现。可以自主地改变生产者,消费者的数目,和缓冲区。
多线程实现生产者消费者模型:锁(Lock)、信号量(Semaphore、BoundedSemaphore)、条件(Condition)、队列(Queue)、事件(Event) 多进程程实现生产者消费者模型:信号量(Semaphore)、条件(Condition)、...
生产者与消费者实验报告生产者与消费者实验报告生产者与消费者实验报告生产者与消费者实验报告
(2)信号量机制实现生产者和消费者对缓冲区的互斥访问。 (3)生产者生产产品时,要输出当前缓冲区冲产品的个数和存放产品的位置。 (4)消费者消费产品时,要输出当前缓冲区冲产品的个数和消费产品的位置。 (5)...
此外,pv操作还可以广泛应用于各种生产者消费者问题的解决中,使得系统更为可靠和高效。 pv操作是解决生产者与消费者问题的常用方法之一。通过pv操作,我们可以确保生产者进程和消费者进程之间的同步,避免数据的...
设计目的:通过研究Linux 的进程机制和信号量实现生产者消费者问题的并发控制。说明:有界缓冲区内设有20 个存储单元,放入/取出的数据项设定为1‐20 这20 个整型数。设计要求:1)每个生产者和消费者对有界缓冲区...
多线程同步互斥 生产者消费者问题 MFC 实现
C语言实现生产者消费者问题,分配具有n个缓冲区的缓冲池,作为共享资源。 定义两个资源型信号量empty 和full,empty信号量表示当前空的缓冲区数量,full表示当前满的缓冲区数量。 定义互斥信号量mutex,当某个进程...
生产者 消费者 模式 c++ 算是老外写的一个使用demo 可以参考一下
参考教材中的生产者消费者算法,创建5个进程,其中两个进程为生产者进程,3个进程为消费者进程。一个生产者进程试图不断地在一个缓冲中写入大写字母,另一个生产者进程试图不断地在缓冲中写入小写字母。3个消费者...
LinuxC语言实现生产者和消费者模型LinuxC语言实现生产者和消费者模型LinuxC语言实现生产者和消费者模型LinuxC语言实现生产者和消费者模型LinuxC语言实现生产者和消费者模型LinuxC语言实现生产者和消费者模型LinuxC...
操作系统实验课实现的生产者消费者模型图形化演示。通过“企鹅吃苹果”这个小故事吧,苹果是生产者生产出来的,而企鹅是消费者。可以调节生产和消费的速度,也可以暂停程序方便演示。用C++实现的,使用的是纯粹的API...