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. Spring3.0 入门进阶(1):从配置文件装载Bean

    Spring 已经盛行多年,目前已经处于3.0阶段,关于Spring的概念介绍性的东西网上已经很多,本系列博客主要是把一些知识点通过代码的方式总结起来,以便查阅. 作为入门,本篇主要介绍Bean的加载 ...

  2. HTML语言简单回顾

    简单复习一下html语言. html的基本结构如下: <html> <head> <title></title> </head> <b ...

  3. Spring MVC RedirectAttributes的用法解决办法

    Spring MVC RedirectAttributes的用法很久没发过技术贴了,今天对于一个问题纠结了2小时,遂放弃研究用另一种方法解决,奈何心中一直存在纠结,发帖求解 我先解释下什么是Redir ...

  4. HDU 4868 Information Extraction(2014 多校联合第一场 H)

    看到这道题时我的内心是奔溃的,没有了解过HTML,只能靠窝的渣渣英语一点一点翻译啊TT. Information Extraction 题意:(纯手工翻译,有些用词可能在html中不是一样的,还多包涵 ...

  5. js获取当前日期,网页头部用

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  6. 被忽视的META标签之特效

    在web设计中使用js可以实现很多的页面特效,然而很多人却忽视了HTML标签中META标签的强大功效,其实meta标签也可以实现很多漂亮的页面过渡效果. META标签是HTML语言HEAD区的一个辅助 ...

  7. keyset与entryset

    1.基本概述 Set<Map.Entry<K,V>> entrySet()  返回此映射中包括的映射关系的 set 视图. Set<K>              ...

  8. Android开发者指南-用户界面-拖放-Drag and Drop[原创译文]

      英文原文:http://developer.android.com/guide/topics/ui/drag-drop.html 版本:Android 4.0 r1 译者注:黄色底色为未决译文 快 ...

  9. 【设计模式】Singleton模式C++实现

    Singleton是设计模式中比较简单的一个.园中的朋友们应该都很熟悉了.前段时间参加xxx外企的面试,和面试官讨论C++的时候正好写了一个.当时由于在有些地方考虑不太周全,代码出现了一些疏漏.不过最 ...

  10. xcode6 cocos2dx开玩笑git和github学习记录

    1. git Xcode4开始,它一直Git作为一个内置的源代码控制(Source Control)工具,所以对于新项目的用途git要管理非常方便.在新建项目向导.可以直接选择Git作为源控制工具.项 ...