• 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
    
    **/
    

  • 相关阅读:
    OpenStack入门篇(八)之镜像服务Glance
    OpenStack入门篇(七)之认证服务Keystone
    OpenStack入门篇(六)之OpenStack环境准备
    OpenStack入门篇(五)之KVM性能优化及IO缓存介绍
    OpenStack入门篇(四)之KVM虚拟机介绍和管理
    OpenStack入门篇(三)之KVM介绍及安装
    OpenStack入门篇(二)之OpenStack架构
    OpenStack入门篇(一)之云计算的概念
    Linux系统运维基础管理命令总结
    TCP三次握手和四次挥手以及11种状态
  • 原文地址:https://www.cnblogs.com/hrhguanli/p/4003511.html
Copyright © 2020-2023  润新知