• ReentrantLock Condition 实现消费者生产者问题


    import java.util.LinkedList;
    import java.util.Queue;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.locks.Condition;
    import java.util.concurrent.locks.ReentrantLock;
    
    //产品
    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();
    			} 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 Buffer2
    { private Queue<Product>  queque=new LinkedList<Product>();
    private int size=5;
    private ReentrantLock lock=new ReentrantLock(true);
    private Condition notFull=lock.newCondition();
    private Condition notEmpty=lock.newCondition();
    public void add(Product p)
    {
    	lock.lock();
    	try
    	{
    	while(queque.size()==size)
    	{
    		
    		
    			notFull.await();
    	}
    		
    	
    	queque.add(p);
    	notEmpty.signal();
    	} catch (InterruptedException e) {
    		// TODO Auto-generated catch block
    		e.printStackTrace();
    	}finally
    	{
    	
    	lock.unlock();
    	}
    	
    }
    public void remove() 
    {
    	try {
    		lock.lockInterruptibly();
    	} catch (InterruptedException e) {
    		// TODO Auto-generated catch block
    		e.printStackTrace();
    	}
    	try
    	{
    		while(queque.size()==0) notEmpty.await();
    		queque.poll();
    		notFull.signal();
    		
    	} catch (InterruptedException e) {
    		// TODO Auto-generated catch block
    		e.printStackTrace();
    	}finally{
    		lock.unlock();
    		
    	}
    	
    	
    	
    }
    
    
    
    
     
    	
    
    }
    
    class Producer implements Runnable
    {
    	private Buffer2 buf;
    	
    	public Producer(Buffer2 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 Buffer2 buf=null;
      public Customer(Buffer2 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
    		
    		//学学使用线程池
    		Buffer2 buf=new Buffer2();
    		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();
    
    	}
    
    }
    

      

  • 相关阅读:
    DevExpress的GridControl的实时加载数据解决方案(取代分页)
    WinForm程序虚拟分页(实时加载数据)
    C#使用反射特性构建访问者模式
    WinApi学习笔记内存的复制,填充,输出等操作
    PL/SQL学习笔记程序包
    WinApi学习笔记创建进程
    PL/SQL学习笔记触发器
    WinApi学习笔记获取光驱中的信息
    WinApi学习笔记读写文件
    不通过配置文件启动WCF服务
  • 原文地址:https://www.cnblogs.com/hansongjiang/p/3870332.html
Copyright © 2020-2023  润新知