zoukankan      html  css  js  c++  java
  • 使用Notify 和 wait ,使用Linklist实现生产者消费者问题

    ref:http://www.cnblogs.com/happyPawpaw/archive/2013/01/18/2865957.html

    注释很清楚的,

    import java.util.LinkedList;
    import java.util.Queue;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    
    //产品
    class Product
    {
    	 String name=null;
    	public Product(String name)
    	{
    		this.name=name;
    	}
    	
    }
    
    class  Buffer
    {
      private Queue<Product> queue=new LinkedList<Product>();//一个普通队列,同时作为同步的对象
      private final int size=5;  //最大长度为,可以自己调整
      public void add(Product p)//
      {
    	  synchronized (queue) {
    		  while(queue.size()==size)
    		  {
    			  System.out.println(Thread.currentThread().getName()+"队列已经满了,生产者释放锁");
    			  try {
    				queue.wait(); //队列未满,则放弃锁,进行挂起,等到queue,notify,有个线程会重新从wait后运行
    			} catch (InterruptedException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		  }
    		  queue.offer(p);
    		 
    		  System.out.println(Thread.currentThread().getName()+"入队    "+queue.size());
    		  
    		  queue.notify();//加入一个元素,则通因为为空而挂起的线程
    		
    	}
    	  
     }
      
      public void remove()
      {
    	  synchronized (queue) {
    		  while(queue.size()<=0)
    		  {
    
    			  System.out.println(Thread.currentThread().getName()+"队列为空,释放锁");
    			  try {
    				queue.wait();
    			} catch (InterruptedException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		  }
    		  queue.poll();
    		  System.out.println(Thread.currentThread().getName()+"出队,剩下"+queue.size());
    		  queue.notify();
    		
    	}
    	  
      }
    	
    
    }
    
    class Producer implements Runnable
    {
    	private Buffer buf; //所有线程共享一个buffer,所以作为参数
    	
    	public Producer(Buffer buf)
    	{
    		this.buf=buf;
    	}
    
    	@Override
    	public void run() {
    		// TODO Auto-generated method stub
    		for(int i=0;i<10;i++)
    		{
    			try {
    				Thread.sleep(3000);  //控制生产速度,可以让生产快,还是消费快,来观察队列的情况,
    			} catch (InterruptedException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    			buf.add(new Product("zhang "+i));
    		}
    		
    		
    	}
    	
    
    
    }
    class Customer implements Runnable
    {
      private Buffer buf=null;
      public Customer(Buffer buf)
      {
    	  this.buf=buf;
      }
    	@Override
    	public void run() {
    		for(int i=0;i<10;i++)
    		{
    			try {
    				Thread.sleep(1);//控制生产速度,,
    			} catch (InterruptedException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}//
    			buf.remove();
    		}
    		
    	}
    	
    
    }
    
    public class 生产消费者 {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		
    		//学学使用线程池
    		Buffer buf=new Buffer();
    		ExecutorService exe=Executors.newCachedThreadPool();//一个简单的线程池
    		int i=0;
    		while(i++<2)
    		{
    			exe.submit(new Producer(buf)); //生产者两个线程
    			
    		}
    		i=0;
    		while(i++<2)
    		{
    			exe.submit(new Customer(buf));//消费者两个线程
    		}
    		exe.shutdown();
    
    	}
    
    }
    
  • 相关阅读:
    關於GoogleUpdate.exe
    [ZT]网站优化之页面优化重新整理
    [ZT]企业建站常用中英文对照表
    "因為整合式windows驗證沒有啟用"解決方式
    模态与非模态的弹出窗口
    [ZT]ASP.Net常用正则表达式
    【ZT】基于.Net的SAP Portal开发
    .Net Quick start for the OpenFlashChart control
    微软将发布10款假补丁 测试Windows 7升级机制
    【ZT】Oracle初学者必知的100个问题
  • 原文地址:https://www.cnblogs.com/hansongjiang/p/3859238.html
Copyright © 2011-2022 走看看