java 线程 ProducerAndConsumer
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的更多相关文章
- Java线程并发:知识点
Java线程并发:知识点 发布:一个对象是使它能够被当前范围之外的代码所引用: 常见形式:将对象的的引用存储到公共静态域:非私有方法中返回引用:发布内部类实例,包含引用. 逃逸:在对象尚未准备 ...
- Java线程的概念
1. 计算机系统 使用高速缓存来作为内存与处理器之间的缓冲,将运算需要用到的数据复制到缓存中,让计算能快速进行:当运算结束后再从缓存同步回内存之中,这样处理器就无需等待缓慢的内存读写了. 缓 ...
- Java 线程池框架核心代码分析--转
原文地址:http://www.codeceo.com/article/java-thread-pool-kernal.html 前言 多线程编程中,为每个任务分配一个线程是不现实的,线程创建的开销和 ...
- 细说进程五种状态的生老病死——双胞胎兄弟Java线程
java线程的五种状态其实要真正高清,只需要明白计算机操作系统中进程的知识,原理都是相同的. 系统根据PCB结构中的状态值控制进程. 单CPU系统中,任一时刻处于执行状态的进程只有一个. 进程的五种状 ...
- 【转载】 Java线程面试题 Top 50
Java线程面试题 Top 50 不管你是新程序员还是老手,你一定在面试中遇到过有关线程的问题.Java语言一个重要的特点就是内置了对并发的支持,让Java大受企业和程序员 的欢迎.大多数待遇丰厚的J ...
- 第24章 java线程(3)-线程的生命周期
java线程(3)-线程的生命周期 1.两种生命周期流转图 ** 生命周期:**一个事物冲从出生的那一刻开始到最终死亡中间的过程 在事物的漫长的生命周期过程中,总会经历不同的状态(婴儿状态/青少年状态 ...
- 第23章 java线程通信——生产者/消费者模型案例
第23章 java线程通信--生产者/消费者模型案例 1.案例: package com.rocco; /** * 生产者消费者问题,涉及到几个类 * 第一,这个问题本身就是一个类,即主类 * 第二, ...
- 第22章 java线程(2)-线程同步
java线程(2)-线程同步 本节主要是在前面吃苹果的基础上发现问题,然后提出三种解决方式 1.线程不安全问题 什么叫线程不安全呢 即当多线程并发访问同一个资源对象的时候,可能出现不安全的问题 对于前 ...
- 第21章 java线程(1)-线程初步
java线程(1)-线程初步 1.并行和并发 并行和并发是即相似又有区别: 并行:指两个或者多个事件在同一时刻点发生. 并发:指两个或多个事件在同一时间段内发生 在操作系统中,并发性是指在一段事件内宏 ...
随机推荐
- xcode-build/version-bump
# xcode-build-bump.sh # @desc Auto-increment the build number every time the project is run. # @usag ...
- Gitolite v3安装配置指南
使用gitolite对git仓储进行权限配置 gitolite在近期做了很多代码改动,升级到了v3版本,而我使用的是v3.5.2.在<Git权威指南>中所提及的是v2版本,有很多东西已经不 ...
- HBase零基础高阶应用实战(CDH5、二级索引、实践、DBA)
HBase是一个分布式的.面向列的开源数据库,该技术来源于 Fay Chang 所撰写的Google论文“Bigtable:一个结构化数据的分布式存储系统”.就像Bigtable利用了Google文件 ...
- Coreseek:第二步建索引及測试
1,建索引非常easy.一行代码 g:/service/coreseek/bin/indexer -c g:/service/coreseek/etc/csft_mysql.conf person ...
- MySql自己定义排序
查询语句: select id,name,stauts from special where id in (50,51,52,53,54,55) order by FIELD( id ,51,50, ...
- CABasicAnimation学习Demo 包含了一些经常使用的动画效果
个人写的一些样例: // // ViewController.m // CABasicAnimationDemo // // Created by haotian on 14-6-13. // Cop ...
- 怎样配置PHP环境和安装Zendstdio编辑器
想学习PHP好久了.苦于环境配置不好,一直感觉无从下手. 在网上找了个视频: 李炎恢PHP教程 第一章前3节给出了具体的配置的方法,即安装Apache和Zendstudio 10.5仅仅须要照着视频做 ...
- 与众不同 windows phone (6) - Isolated Storage(独立存储)
原文:与众不同 windows phone (6) - Isolated Storage(独立存储) [索引页][源码下载] 与众不同 windows phone (6) - Isolated Sto ...
- MySQL 採用Xtrabackup对数据库进行全库备份
1,xtrabackup简单介绍 关于数据库备份以及备份工具.參考:http://blog.itpub.net/26230597/viewspace-1460065/,这里来介绍xtrabackup已 ...
- JavaScript权威指南科20章 client记忆
20 client记忆 client几种形式存储的: web记忆 cookie IE userData 离线应用 web数据库 文件系统api 20.1 localStorage 和 sessionS ...