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. 使用JDBC处理数据库大容量数据类型

    在本文将介绍如何使用JDBC操作MySQL数据库对于大容量数据类型的读取.在之前的博客中已经介绍了如何使用JDBC来操作数据库对各种数据的增删改查,那么大容量数据类型的数据操作又为何不同呢. 原因在于 ...

  2. 查看 ios 真机调试log,导出log

    使用Xcode 在模拟器李敏运行的时候,可以直接通过xcode 查看log,但是真机测试的时候,xcode 却无法获取到,对于日志输出,可以先保存到真机上,之后通过iTunes 导出即可 修改源码 此 ...

  3. 终于懂了:WM_PAINT 与 WM_ERASEBKGND(三种情况:用户操作,UpdateWindow,InvalidateRect产生的效果并不相同),并且用Delphi代码验证 good

    一直对这两个消息的关系不是太了解,借重新深刻学习windows编程的机会研究一番. 1)当窗口从无效变为有效时,比方将部分覆盖的窗口恢复时会重绘窗口时:程序首先会通过发送其他消息调用DefWindow ...

  4. Fedora Linux 下安装配置C开发环境Code::Blocks

    一.提前的话要说C语言和Linux的关系大家应该都不会陌生,Linux系统内核就是用C语言开发的,所以所有的Linux系统下面 都会有C的编译调试工具,不过这些工具都是命令式的,正式开发的话会很不方便 ...

  5. openfire学习4------->android客户端聊天开发之聊天功能开发

    前面我们已经把服务器搭建完成,并且在客户端实现了登录了. 和我们使用的QQ一样,想一想,登录成功之后呢?肯定是要有一个好友列表,通过这个列表,我们可以选择我们需要聊天的好友. 这里我们先研究下 xmp ...

  6. Swift - 生成各种控件的工厂类(包含标签,按钮,输入框等)

    在iOS开发中,页面里有时会大量的用到一些控件,如果要一个个单独创建再设置样式的话就显得很麻烦.我们可以创建一个生成各种控件的工厂类,这样在需要的时候调用下就可以了. 下面以一个自定义的工厂类为例,其 ...

  7. Find the minimum线段树成段更新

    问题 G: Find the minimum 时间限制: 2 Sec   内存限制: 128 MB 提交: 83   解决: 20 [ 提交][ 状态][ 讨论版] 题目描述 Given an int ...

  8. POJ1422 Air Raid 【DAG最小路径覆盖】

    Air Raid Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6763   Accepted: 4034 Descript ...

  9. svn自动发用户名密码到邮件(明文密码)

    #!/bin/sh touch testlist cat /dev/null > testlist grep "=" passwd |grep -v "#" ...

  10. Allegro CL Express Edition Downloads

    Allegro CL Express Edition Downloads Allegro CL Express Edition Downloads