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

**/
时间: 2024-07-28 17:39:18

java 线程 ProducerAndConsumer的相关文章

java 线程详解

5月7号  周末看了一下线程方面的内容 ,边看视频边看书还附带着参考了很多人的博客,一天的收获,写下来整理一下:感觉收获还是挺多的:过段时间可能看完java  这几大块要去看一下关于spring boot  的内容顺便  也整理一下:附上我参考的 几本书: 关于java  线程,首先要了解一下线程和进程之间的关系.区别以及他们之间的概念: 首先是线程: 什么是线程? 线程是在程序执行过程中能够执行部分代码的一个执行单元,也看看做是一个轻量级的进程:线程是程序内的程序控制流只能使用程序内分配给程序

Java线程工作内存与主内存变量交换过程及volatile关键字理解

Java线程工作内存与主内存变量交换过程及volatile关键字理解 1. Java内存模型规定在多线程情况下,线程操作主内存变量,需要通过线程独有的工作内存拷贝主内存变量副本来进行.此处的所谓内存模型要区别于通常所说的虚拟机堆模型: 2. 线程独有的工作内存和进程内存(主内存)之间通过8中原子操作来实现,如下图所示: 原子操作的规则(部分): 1) read,load必须连续执行,但是不保证原子性. 2) store,write必须连续执行,但是不保证原子性. 3) 不能丢失变量最后一次ass

java线程

Java线程详解 1.操作系统中的线程和进程讲解: 现在的操作系统大都是多任务操作系统,多线程是多任务的一种. 进程是指操作系统中运行的一个程序,每个进程都有自己的一块内存空间,一个进程中可以启动多个线程. 线程是指进程中的一个执行流程,一个进程中可以运行多个线程.比如java.exe进程中可以运行很多线程.线程总是属于某个进程,进程中的多个线程共享进程的内存. “同时”执行是人的感觉,在线程之间实际上轮换执行. Java线程的两种具体实现方法: 第一种继承:具体代码实现如下: Public (

Java 线程第三版 第四章 Thread Notification 读书笔记

一.等待与通知 public final void wait() throws InterruptedException 等待条件的发生. public final void wait(long timeout) throws InterruptedException 等待条件的发生.如果通知没有在timeout指定的时间内发生,它还是会返回. public final void wait(long timeout, int nanos) throws InterruptedException

Java线程使用大全

1.线程实现 1.Thread类 构造方法: 案例代码: public class Ex10_1_CaseThread extends Thread {// 创建一个类继承(extend)Thread类 String studentName; public Ex10_1_CaseThread(String studentName) {// 定义类的构造函数,传递参数 System.out.println(studentName + "申请访问服务器"); this.studentNam

java线程五种状态

java线程五种状态: 创建 -> 就绪 -> 运行 -> 销毁 创建 -> 就绪 -> 运行 -> 等待(缺少资源) -> 销毁 下图:各种状态转换

java线程详细介绍

目录(?)[-] 一扩展javalangThread类 二实现javalangRunnable接口 三Thread和Runnable的区别 四线程状态转换 五线程调度 六常用函数说明 使用方式 为什么要用join方法 七常见线程名词解释 八线程同步 九线程数据传递 本文主要讲了java中多线程的使用方法.线程同步.线程数据传递.线程状态及相应的一些线程函数用法.概述等. 首先讲一下进程和线程的区别: 进程:每个进程都有独立的代码和数据空间(进程上下文),进程间的切换会有较大的开销,一个进程包含1

java 线程通信

java 线程通信使用wait notify 配合synchronized 当线程执行wait()时,会把当前的锁释放,然后让出CPU,进入等待状态.当执行notify/notifyAll方法时,会唤醒一个处于等待该 对象锁 的线程,然后继续往下执行,直到执行完退出对象锁锁住的区域(synchronized修饰的代码块)后再释放锁. 如下代码: public class ThreadTest { //声明一个线程可视化的list集合 public static List<String> lis

分享一个java线程专栏

专栏 : java线程基础 转载自 http://blog.csdn.net/column/details/yinwenjiethread.html 专栏内容: 1.线程基础:线程(1)--操作系统和线程原理 2.线程基础:线程(2)--JAVA中的基本线程操作(上) 3. 线程基础:线程(3)--JAVA中的基本线程操作(中) 4.线程基础:线程(4)--JAVA中的基本线程操作(下) 5.线程基础:线程池(5)--基本使用(上) 6.线程基础:线程池(6)--基本使用(中) 7.线程基础:线