- 浏览: 462433 次
- 性别:
- 来自: 杭州
文章分类
- 全部博客 (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个产品,并消费完毕。
结论: 当使用一个生产者,一个消费者是,需要4610,使用7个生产者,7个消费者时,只需要900,可见当N个任务耗时长,使用多线程可以充分发挥多核CPU的优点.
what do you care?
结论: 当使用一个生产者,一个消费者是,需要4610,使用7个生产者,7个消费者时,只需要900,可见当N个任务耗时长,使用多线程可以充分发挥多核CPU的优点.
package ycl.learn.effective.java.thread.pc; /** * 公共资源类 */ public class PublicResource { private int number = 0; private int index = 0; public static final int MAX_POOL = 10; public static final Product[] products = new Product[MAX_POOL]; public static final int MAX_RESOURCE = 20; /** * 增加公共资源 */ public synchronized boolean increace() { if(isComplete()){//生产任务完成 return false; } while (isFull()) { try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } number++; index++; Product p = new Product(index); products[number-1]=p; notifyAll(); System.out.println(Thread.currentThread()+"increace:" + index); return true; } /** * 减少公共资源 */ public synchronized boolean decreace() { while (isEmpty()) { try { if(isComplete()){//如果消费结束,将会死锁,因为isEmpty()is true forever, 所以不管你如何notify,他始终等待,添加当wait被notify时,消费任务完成,退出。 return false; } wait(); } catch (InterruptedException e) { e.printStackTrace(); } } Product p =products[number-1]; number--; notifyAll(); System.out.println(Thread.currentThread()+"decreace:" + p.index); return true; } /** * 判断是否满了 */ public boolean isFull() { return number >= MAX_POOL; } /** * 判断是否空了 */ public boolean isEmpty() { return number <= 0; } /** * 判断生产完毕 * */ public boolean isComplete() { return index >= MAX_RESOURCE; } class Product{ public int index; public Product(int index){ this.index = index; } } }
package ycl.learn.effective.java.thread.pc; public class Productor implements Runnable { private PublicResource ps; public Productor(PublicResource ps) { this.ps = ps; } public void run() { while (true) { boolean flag = ps.increace(); if(!flag){ System.out.println(Thread.currentThread()+" exist"); break; } try { Thread.sleep((int) (Math.random() * 500)); } catch (InterruptedException e) { e.printStackTrace(); } } } }
package ycl.learn.effective.java.thread.pc; public class Customer implements Runnable { private PublicResource ps; public Customer(PublicResource ps) { this.ps = ps; } public void run() { while (true) { boolean flag = ps.decreace(); if(!flag){ System.out.println(Thread.currentThread()+" exist"); break; } try { Thread.sleep((int) (Math.random() * 500)); } catch (InterruptedException e) { e.printStackTrace(); } } } }
package ycl.learn.effective.java.thread.pc; public class TestPC { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub one(); } public static void one() throws InterruptedException{ long start = System.currentTimeMillis(); PublicResource ps = new PublicResource(); Customer c = new Customer(ps); Productor p = new Productor(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);//4968 } }
Thread[productor0,5,main]increace:1 Thread[customer5,5,main]decreace:1 Thread[productor2,5,main]increace:2 Thread[customer0,5,main]decreace:2 Thread[productor0,5,main]increace:3 Thread[customer4,5,main]decreace:3 Thread[productor1,5,main]increace:4 Thread[productor4,5,main]increace:5 Thread[customer1,5,main]decreace:5 Thread[customer3,5,main]decreace:4 Thread[productor3,5,main]increace:6 Thread[customer6,5,main]decreace:6 Thread[productor6,5,main]increace:7 Thread[customer2,5,main]decreace:7 Thread[productor5,5,main]increace:8 Thread[customer6,5,main]decreace:8 Thread[productor2,5,main]increace:9 Thread[customer3,5,main]decreace:9 Thread[productor4,5,main]increace:10 Thread[customer4,5,main]decreace:10 Thread[productor6,5,main]increace:11 Thread[customer5,5,main]decreace:11 Thread[productor1,5,main]increace:12 Thread[customer0,5,main]decreace:12 Thread[productor1,5,main]increace:13 Thread[customer2,5,main]decreace:13 Thread[productor6,5,main]increace:14 Thread[customer3,5,main]decreace:14 Thread[productor1,5,main]increace:15 Thread[customer1,5,main]decreace:15 Thread[productor1,5,main]increace:16 Thread[customer2,5,main]decreace:16 Thread[productor5,5,main]increace:17 Thread[customer3,5,main]decreace:17 Thread[productor2,5,main]increace:18 Thread[customer4,5,main]decreace:18 Thread[productor0,5,main]increace:19 Thread[customer4,5,main]decreace:19 Thread[productor3,5,main]increace:20 Thread[productor5,5,main] exist Thread[productor4,5,main] exist Thread[customer6,5,main]decreace:20 Thread[productor1,5,main] exist Thread[customer1,5,main] exist Thread[customer5,5,main] exist Thread[productor0,5,main] exist Thread[customer0,5,main] exist Thread[customer4,5,main] exist Thread[productor6,5,main] exist Thread[productor2,5,main] exist Thread[customer6,5,main] exist Thread[productor3,5,main] exist Thread[customer2,5,main] exist Thread[customer3,5,main] exist 875
评论
2 楼
a123159521
2014-04-10
putonyuer 写道
画图用了多长时间?》
what do you care?
1 楼
putonyuer
2014-04-07
画图用了多长时间?》
发表评论
-
Java Application Cache
2016-09-27 19:25 830Application Cache is used very ... -
Java 字符串分词
2015-01-02 14:43 1682在Java的世界里有个类型 ... -
jdk 1.6 新特性,集成Groovy, 性能很差
2014-04-02 14:27 1226性能都是相对的,如果调用量不是很大的话,可以忽略,毕竟使用为主 ... -
Fake Code easy implements
2014-04-01 15:41 990package org.miniframe.modules ... -
JDK regex 用法及用途
2014-03-31 15:48 1166查找 Boolean flag = pattern.mat ... -
生产者消费者(四)
2014-03-04 12:32 1105需求: 多个生产者不断的生产产品,多个消费者不断的消费产品,仓 ... -
生产者消费者(三)
2014-03-04 10:59 922需求: 多个生产者不断的生产产品,多个消费者不断的消费产品,仓 ... -
生产者消费者(二)
2014-03-03 15:40 639需求: 多个生产者不断的生产产品,多个消费者不断的消费产品,仓 ... -
查看Class文件使用的JDK版本
2013-10-30 14:17 1073由于JDK一般是向下兼容的,所以有时候本地的JDK版本比类库的 ... -
Java源代码转码
2012-12-20 17:22 1283现在中国的项目很多,编码无非是UTF-8,GBK,GB2312 ... -
Tomcat集成OSGI,并通过JNDI开放Web调用
2012-12-03 11:22 3082Tomcat集成OSGi,首先要选择OSGI服务器,我这里采用 ... -
JDK的Logging
2012-11-07 15:49 1651jdk自带有一个log日志,对于一般的使用,仅够了. 代码如下 ... -
java.util.*
2012-11-06 14:23 1332java.util 工具包,灰常的有用,有机会一定要研读源码。 ... -
java.util.concurrent.*
2012-11-02 10:38 17321. java.util.concurrent.ArrayBl ... -
java.util.rt.*
2012-10-31 13:51 10741. java.util.HashMap 散列表,主要是以离散 ... -
巧秒设计方法,不返回null
2016-09-27 19:32 650/** * {@inheritDoc} * ... -
java doc 代码文档
2012-07-13 13:58 1278对于代码规范不解释了,网上很多。 在编写代码的时候,有一点灰 ... -
接口与抽象类
2012-07-11 16:53 10901. 接口设计必谨慎,除非业务变更,否则打死不能动接口。[不变 ... -
JVM优化机制好诡异
2012-04-20 08:43 1424long i[] = new long[1000000]; ... -
JVM优化机制好诡异
2016-09-27 19:32 527long i[] = new long[100000 ...
相关推荐
生产者 消费者 模式 c++ 算是老外写的一个使用demo 可以参考一下
生产者消费者模式.doc生产者消费者模式.doc生产者消费者模式.doc
labview 基于事件的生产者消费者模式
使用多线程实现了生产者和消费者模式
使用Qt 和 C++11 的std::mutex 和 std::condition_variable 实现一个演示生产者消费者模式的Qt工程。
自己空闲时间写的一个生产者和消费者模式。中间使用了中介者设计模式。大家可以参考下。
生产者消费者synchronized实现方式
用java多线程简易实现生产者消费者模式的代码,用java多线程简易实现生产者消费者模式的代码。
C语言实现生产者消费者问题,分配具有n个缓冲区的缓冲池,作为共享资源。 定义两个资源型信号量empty 和full,empty信号量表示当前空的缓冲区数量,full表示当前满的缓冲区数量。 定义互斥信号量mutex,当某个进程...
java 多线程 生产者消费者模式,你值得拥有
redis 老版本生产者消费者模式
架构设计—生产者消费者模式 讲得比较透彻
界面化模拟生产者消费者模式,生产者生产数据数量和消费者消费数据数量可自定义输入,仓库内最大数量和最小剩余量以及生产者每次生产数量和消费者每次消费数量都可以自定义输入
LinuxC语言实现生产者和消费者模型LinuxC语言实现生产者和消费者模型LinuxC语言实现生产者和消费者模型LinuxC语言实现生产者和消费者模型LinuxC语言实现生产者和消费者模型LinuxC语言实现生产者和消费者模型LinuxC...
delphi多线程生产者消费者模式源程序
java 多线程 生产者消费者模式,多个生产者对多个消费者,使用jdk 线程池及 BlockingQueue实现,解决了待生产的任务生产完成后,正常终止所有线程,避免线程(特别是消费者线程)因阻塞而无限等待的情况。源码中还简单...
读《深入理解计算机系统》后,参照书中程序,用C代码实现生产者消费者模型,本代码可移植性强,各接口以封装好,并有注释
这是本人学习labview生产者消费者模型时,收藏的资料,里面包含生产消费者模型的文档介绍和例程,保证资源真实有效,例程能跑。
在实际的软件开发过程中,经常会碰到如下场景:某个模块负责产生数据,这些数据由另一个模块来负责处理(此处的模块是广义的,可以是类、函数...产生数据的模块,就形象地称为生产者;而处理数据的模块,就称为消费者。
基于生产消费者模式的多串口采集程序,用labview进行上位机处理(Serial port acquisition program, based on the pattern of production of consumer PC processing using labview)