package j2se.thread.demo;
/**
* <p>Project:J2SE 的基础知识</p>
* <p>Tile:多线程模拟 生产者 和 消费者 </p>
* <p>Description:
*
*
* </p>
*
* @date 2014-06-10
* @author liwenkai
* @version 1.0
*
*/
public class ProducerAndConsumer { public static void main(String[] args){
Stack s = new Stack(6) ;
Producer p = new Producer(s) ;
Consumer c = new Consumer(s) ;
Thread tp = new Thread(p,"产品") ;
tp.start() ;
try {
// 主线程 main 先 sleep 100 millis , 让生产者先创建产品
Thread.sleep(100) ;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Thread tc = new Thread(c,"产品") ;
tc.start() ;
}
} // 定义产品类
class Item
{
public String id ;
Item(String _id){
id = _id ;
}
}; // 模拟一个 stack (栈) 容器
class Stack
{
Item[] arr ;
int index ;
Stack(int _size){
arr = new Item[_size] ;
index = 0 ;
} public synchronized void push(Item _item){ while (index == arr.length )
{
try
{
// index == arr.length 生产线程 wait , 堵塞掉
this.wait() ;
}
catch (InterruptedException e)
{
e.printStackTrace() ;
} }
this.notify() ;
arr[index] = _item ;
index++ ;
// 在这里 读取 的 index 最准确
System.out.println("push consumer index = " + index
+ " , current time = "+System.currentTimeMillis()) ;
} public synchronized Item pop(){ while( index == 0)
{
try
{
// 当 index == 0 的时候 , 消费线程 wait , 堵塞掉 ;
this.wait() ;
}
catch (InterruptedException e)
{
e.printStackTrace() ;
}
}
this.notify() ;
index-- ;
// 在这里 读取 的 index 最准确
System.out.println("pop consumer index = " + index
+ " , current time = "+System.currentTimeMillis()) ;
return arr[index] ; } public synchronized int getIndex() {
return index;
} }; // 生产者
class Producer implements Runnable{
Stack s ; Producer(Stack _s){
s = _s ;
} public void run(){ for ( int i = 0; i < 30 ; i++ )
{
Item item = new Item(Thread.currentThread().getName() + i) ;
s.push(item) ;
// 这里拿到的 stack.index = s.getIndex() , 可能不是生产者线程改动之后的数值 ,
// 由于消费者线程也在执行 , 也能够改动这个数值 ;
System.out.println("生产了产品\"" + item.id
+ "\" 这是"+Thread.currentThread().getName() +"生产的第" + i + "个产品 , "
+ "stack.index = " + s.getIndex()
+ " , curent time = " +System.currentTimeMillis()) ;
try
{
Thread.sleep((int)(Math.random()*1000)) ;
}
catch (InterruptedException e)
{
e.printStackTrace() ;
} }
} } // 消费者
class Consumer implements Runnable{
Stack s ; Consumer(Stack _s){
s = _s ;
} public void run(){ for (int i = 0 ; i < 30 ; i++ )
{
Item item = s.pop() ;
// 这里拿到的 stack.index = s.getIndex() , 可能不是消费者线程改动之后的数值 ,
// 由于生产者线程也在执行 , 也能够改动这个数值 ;
System.out.println("消费了产品\"" + item.id + "\" 这是"+Thread.currentThread().getName()
+"消费的第" + i + "个产品" + " , "
+ "stack.index = " + s.getIndex()
+ " , current time = "+System.currentTimeMillis()) ;
try
{
Thread.sleep((int)(Math.random()*1000)) ;
}
catch (InterruptedException e)
{
e.printStackTrace() ;
}
} } } /**
*
push consumer index = 1 , current time = 1402393225000
生产了产品"产品0" 这是产品生产的第0个产品 , stack.index = 1 , curent time = 1402393225000
pop consumer index = 0 , current time = 1402393225109
消费了产品"产品0" 这是产品消费的第0个产品 , stack.index = 0 , current time = 1402393225109
push consumer index = 1 , current time = 1402393225718
pop consumer index = 0 , current time = 1402393225718
消费了产品"产品1" 这是产品消费的第1个产品 , stack.index = 0 , current time = 1402393225718
生产了产品"产品1" 这是产品生产的第1个产品 , stack.index = 0 , curent time = 1402393225718
push consumer index = 1 , current time = 1402393226718
pop consumer index = 0 , current time = 1402393226718
消费了产品"产品2" 这是产品消费的第2个产品 , stack.index = 0 , current time = 1402393226718
生产了产品"产品2" 这是产品生产的第2个产品 , stack.index = 0 , curent time = 1402393226718
push consumer index = 1 , current time = 1402393227218
生产了产品"产品3" 这是产品生产的第3个产品 , stack.index = 1 , curent time = 1402393227218
pop consumer index = 0 , current time = 1402393227453
消费了产品"产品3" 这是产品消费的第3个产品 , stack.index = 0 , current time = 1402393227453
push consumer index = 1 , current time = 1402393228109
pop consumer index = 0 , current time = 1402393228109
生产了产品"产品4" 这是产品生产的第4个产品 , stack.index = 0 , curent time = 1402393228109
消费了产品"产品4" 这是产品消费的第4个产品 , stack.index = 0 , current time = 1402393228109
push consumer index = 1 , current time = 1402393228312
生产了产品"产品5" 这是产品生产的第5个产品 , stack.index = 1 , curent time = 1402393228312
pop consumer index = 0 , current time = 1402393228390
消费了产品"产品5" 这是产品消费的第5个产品 , stack.index = 0 , current time = 1402393228390
push consumer index = 1 , current time = 1402393228593
生产了产品"产品6" 这是产品生产的第6个产品 , stack.index = 1 , curent time = 1402393228593
pop consumer index = 0 , current time = 1402393228593
消费了产品"产品6" 这是产品消费的第6个产品 , stack.index = 0 , current time = 1402393228593
push consumer index = 1 , current time = 1402393228890
pop consumer index = 0 , current time = 1402393228890
消费了产品"产品7" 这是产品消费的第7个产品 , stack.index = 0 , current time = 1402393228890
生产了产品"产品7" 这是产品生产的第7个产品 , stack.index = 0 , curent time = 1402393228890
push consumer index = 1 , current time = 1402393229312
生产了产品"产品8" 这是产品生产的第8个产品 , stack.index = 1 , curent time = 1402393229312
pop consumer index = 0 , current time = 1402393229890
消费了产品"产品8" 这是产品消费的第8个产品 , stack.index = 0 , current time = 1402393229890
push consumer index = 1 , current time = 1402393230000
生产了产品"产品9" 这是产品生产的第9个产品 , stack.index = 1 , curent time = 1402393230000
push consumer index = 2 , current time = 1402393230359
生产了产品"产品10" 这是产品生产的第10个产品 , stack.index = 2 , curent time = 1402393230359
pop consumer index = 1 , current time = 1402393230765
消费了产品"产品10" 这是产品消费的第9个产品 , stack.index = 1 , current time = 1402393230765
push consumer index = 2 , current time = 1402393230781
生产了产品"产品11" 这是产品生产的第11个产品 , stack.index = 2 , curent time = 1402393230781
pop consumer index = 1 , current time = 1402393230890
消费了产品"产品11" 这是产品消费的第10个产品 , stack.index = 1 , current time = 1402393230890
push consumer index = 2 , current time = 1402393230921
生产了产品"产品12" 这是产品生产的第12个产品 , stack.index = 2 , curent time = 1402393230921
push consumer index = 3 , current time = 1402393231390
生产了产品"产品13" 这是产品生产的第13个产品 , stack.index = 3 , curent time = 1402393231390
pop consumer index = 2 , current time = 1402393231625
消费了产品"产品13" 这是产品消费的第11个产品 , stack.index = 2 , current time = 1402393231625
push consumer index = 3 , current time = 1402393231656
生产了产品"产品14" 这是产品生产的第14个产品 , stack.index = 3 , curent time = 1402393231656
push consumer index = 4 , current time = 1402393231984
生产了产品"产品15" 这是产品生产的第15个产品 , stack.index = 4 , curent time = 1402393231984
push consumer index = 5 , current time = 1402393232125
生产了产品"产品16" 这是产品生产的第16个产品 , stack.index = 5 , curent time = 1402393232125
push consumer index = 6 , current time = 1402393232250
生产了产品"产品17" 这是产品生产的第17个产品 , stack.index = 6 , curent time = 1402393232250
pop consumer index = 5 , current time = 1402393232531
消费了产品"产品17" 这是产品消费的第12个产品 , stack.index = 5 , current time = 1402393232531
push consumer index = 6 , current time = 1402393233234
生产了产品"产品18" 这是产品生产的第18个产品 , stack.index = 6 , curent time = 1402393233234
pop consumer index = 5 , current time = 1402393233421
消费了产品"产品18" 这是产品消费的第13个产品 , stack.index = 5 , current time = 1402393233421
pop consumer index = 4 , current time = 1402393233921
消费了产品"产品16" 这是产品消费的第14个产品 , stack.index = 4 , current time = 1402393233921
push consumer index = 5 , current time = 1402393234218
生产了产品"产品19" 这是产品生产的第19个产品 , stack.index = 5 , curent time = 1402393234218
pop consumer index = 4 , current time = 1402393234828
消费了产品"产品19" 这是产品消费的第15个产品 , stack.index = 4 , current time = 1402393234828
push consumer index = 5 , current time = 1402393235187
生产了产品"产品20" 这是产品生产的第20个产品 , stack.index = 5 , curent time = 1402393235187
push consumer index = 6 , current time = 1402393235609
生产了产品"产品21" 这是产品生产的第21个产品 , stack.index = 6 , curent time = 1402393235609
pop consumer index = 5 , current time = 1402393235796
消费了产品"产品21" 这是产品消费的第16个产品 , stack.index = 5 , current time = 1402393235796
push consumer index = 6 , current time = 1402393235875
生产了产品"产品22" 这是产品生产的第22个产品 , stack.index = 6 , curent time = 1402393235875
pop consumer index = 5 , current time = 1402393236656
消费了产品"产品22" 这是产品消费的第17个产品 , stack.index = 5 , current time = 1402393236656
push consumer index = 6 , current time = 1402393236859
生产了产品"产品23" 这是产品生产的第23个产品 , stack.index = 6 , curent time = 1402393236859
pop consumer index = 5 , current time = 1402393237250
消费了产品"产品23" 这是产品消费的第18个产品 , stack.index = 5 , current time = 1402393237250
push consumer index = 6 , current time = 1402393237812
生产了产品"产品24" 这是产品生产的第24个产品 , stack.index = 6 , curent time = 1402393237812
pop consumer index = 5 , current time = 1402393238187
消费了产品"产品24" 这是产品消费的第19个产品 , stack.index = 5 , current time = 1402393238187
push consumer index = 6 , current time = 1402393238218
生产了产品"产品25" 这是产品生产的第25个产品 , stack.index = 6 , curent time = 1402393238218
pop consumer index = 5 , current time = 1402393238937
push consumer index = 6 , current time = 1402393238937
生产了产品"产品26" 这是产品生产的第26个产品 , stack.index = 6 , curent time = 1402393238937
消费了产品"产品25" 这是产品消费的第20个产品 , stack.index = 6 , current time = 1402393238937
pop consumer index = 5 , current time = 1402393239609
消费了产品"产品26" 这是产品消费的第21个产品 , stack.index = 5 , current time = 1402393239609
pop consumer index = 4 , current time = 1402393239640
消费了产品"产品20" 这是产品消费的第22个产品 , stack.index = 4 , current time = 1402393239640
push consumer index = 5 , current time = 1402393239703
生产了产品"产品27" 这是产品生产的第27个产品 , stack.index = 5 , curent time = 1402393239703
push consumer index = 6 , current time = 1402393239937
生产了产品"产品28" 这是产品生产的第28个产品 , stack.index = 6 , curent time = 1402393239937
pop consumer index = 5 , current time = 1402393240406
消费了产品"产品28" 这是产品消费的第23个产品 , stack.index = 5 , current time = 1402393240406
push consumer index = 6 , current time = 1402393240406
生产了产品"产品29" 这是产品生产的第29个产品 , stack.index = 6 , curent time = 1402393240406
pop consumer index = 5 , current time = 1402393240875
消费了产品"产品29" 这是产品消费的第24个产品 , stack.index = 5 , current time = 1402393240875
pop consumer index = 4 , current time = 1402393241781
消费了产品"产品27" 这是产品消费的第25个产品 , stack.index = 4 , current time = 1402393241781
pop consumer index = 3 , current time = 1402393242703
消费了产品"产品15" 这是产品消费的第26个产品 , stack.index = 3 , current time = 1402393242703
pop consumer index = 2 , current time = 1402393243468
消费了产品"产品14" 这是产品消费的第27个产品 , stack.index = 2 , current time = 1402393243468
pop consumer index = 1 , current time = 1402393243656
消费了产品"产品12" 这是产品消费的第28个产品 , stack.index = 1 , current time = 1402393243656
pop consumer index = 0 , current time = 1402393244375
消费了产品"产品9" 这是产品消费的第29个产品 , stack.index = 0 , current time = 1402393244375 **/

java 线程 ProducerAndConsumer的更多相关文章

  1. Java线程并发:知识点

    Java线程并发:知识点   发布:一个对象是使它能够被当前范围之外的代码所引用: 常见形式:将对象的的引用存储到公共静态域:非私有方法中返回引用:发布内部类实例,包含引用.   逃逸:在对象尚未准备 ...

  2. Java线程的概念

    1.      计算机系统 使用高速缓存来作为内存与处理器之间的缓冲,将运算需要用到的数据复制到缓存中,让计算能快速进行:当运算结束后再从缓存同步回内存之中,这样处理器就无需等待缓慢的内存读写了. 缓 ...

  3. Java 线程池框架核心代码分析--转

    原文地址:http://www.codeceo.com/article/java-thread-pool-kernal.html 前言 多线程编程中,为每个任务分配一个线程是不现实的,线程创建的开销和 ...

  4. 细说进程五种状态的生老病死——双胞胎兄弟Java线程

    java线程的五种状态其实要真正高清,只需要明白计算机操作系统中进程的知识,原理都是相同的. 系统根据PCB结构中的状态值控制进程. 单CPU系统中,任一时刻处于执行状态的进程只有一个. 进程的五种状 ...

  5. 【转载】 Java线程面试题 Top 50

    Java线程面试题 Top 50 不管你是新程序员还是老手,你一定在面试中遇到过有关线程的问题.Java语言一个重要的特点就是内置了对并发的支持,让Java大受企业和程序员 的欢迎.大多数待遇丰厚的J ...

  6. 第24章 java线程(3)-线程的生命周期

    java线程(3)-线程的生命周期 1.两种生命周期流转图 ** 生命周期:**一个事物冲从出生的那一刻开始到最终死亡中间的过程 在事物的漫长的生命周期过程中,总会经历不同的状态(婴儿状态/青少年状态 ...

  7. 第23章 java线程通信——生产者/消费者模型案例

    第23章 java线程通信--生产者/消费者模型案例 1.案例: package com.rocco; /** * 生产者消费者问题,涉及到几个类 * 第一,这个问题本身就是一个类,即主类 * 第二, ...

  8. 第22章 java线程(2)-线程同步

    java线程(2)-线程同步 本节主要是在前面吃苹果的基础上发现问题,然后提出三种解决方式 1.线程不安全问题 什么叫线程不安全呢 即当多线程并发访问同一个资源对象的时候,可能出现不安全的问题 对于前 ...

  9. 第21章 java线程(1)-线程初步

    java线程(1)-线程初步 1.并行和并发 并行和并发是即相似又有区别: 并行:指两个或者多个事件在同一时刻点发生. 并发:指两个或多个事件在同一时间段内发生 在操作系统中,并发性是指在一段事件内宏 ...

随机推荐

  1. nginx+memcached+ftp上传图片+iis

    nginx+memcached+ftp上传图片+iis 自毕业以来,一直在现在公司做订餐系统的开发,那会儿没有口碑,没有饿了么,更别说美团外卖,百度外卖了...因为规模都比较小,都是一个服务器包含数据 ...

  2. 通过Jexus 部署 dotnetcore

    通过Jexus 部署 dotnetcore版本MusicStore 示例程序 ASPNET Music Store application 是一个展示最新的.NET 平台(包括.NET Core/Mo ...

  3. QT学习小技巧

    原地址:http://blog.csdn.net/ykm0722/article/details/6947250 转载: 分享在比赛中写代码时,发现的几个对写程序很有用的小段代码,虽小但是在我的软件中 ...

  4. baas & API 网关

    最近一段时间一直在做API 网关的工作.清晰看到当前云下Baas将会是主要方向,而API网关会是一把利剑. 本人正在规划API网关,有兴趣的可以一起探讨:hotwheels_bo@163.com

  5. 深入研究Clang(四) Clang编译器的简单分析

    作者:史宁宁(snsn1984) 首先我们确定下Clang编译器的详细内容和涵盖范围.之前在<LLVM每日谈之二十 Everything && Clang driver>中 ...

  6. 14.6.2 Moving or Copying InnoDB Tables to Another Machine 移动或者copy InnoDB 表到另外的机器

    14.6.2 Moving or Copying InnoDB Tables to Another Machine 移动或者copy InnoDB 表到另外的机器 这个章节描述技术关于移动或者copy ...

  7. Visual Studio的SDK配置

    Visual Studio的SDK Visual Studio 6.0自带的SDK是1998年的,目录为C:\Program Files\Microsoft Visual Studio\VC98\,这 ...

  8. HealthKit开发教程Swift版:起步

    原文:HealthKit Tutorial with Swift: Getting Started 作者:Ernesto García 译者:Mr_cyz ) HealthKit是iOS 8中的新的A ...

  9. 14.3.2.2 autocommit, Commit, and Rollback 自动提交 提交和回滚

    14.3.2.2 autocommit, Commit, and Rollback 自动提交 提交和回滚 如果自动提交模式被启用,在InnoDB里, 所有的用户活动发生在一个事务里, 每个SQL语句 ...

  10. 从O2O体验活动看华硕平板也“来电”新融合理念

          随着平板电脑的普及,用户对平板的要求也变得越来越高,且这种要求已经逐渐从单纯的性能方面拓展到全方位的功能方面,尤其在通讯.社交.娱乐等层面,平板迫切需要满足用户日益增长的需求.       ...