java 生产者消费者

package com.jin;

import java.util.LinkedList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TestMultipleThread {

	public static void main(String[] args) {
		Production shareList = new Production(); 
		ExecutorService executorService = Executors.newFixedThreadPool(6);
		Consume consume1 = new Consume(shareList);
		Consume consume2 = new Consume(shareList);
		Consume consume3 = new Consume(shareList);
		Consume consume4 = new Consume(shareList);
		ProviderP p1 = new ProviderP(shareList);
		ProviderP p2 = new ProviderP(shareList);
		
		executorService.execute(p1);
		executorService.execute(p2);
		executorService.execute(consume2);
		executorService.execute(consume1);
		executorService.execute(consume3);
		//executorService.execute(consume4);
	}
	
	static class Production {
		LinkedList<String> shareList = new LinkedList<String>(); 
		
		public synchronized void take() {
			if (this.shareList.size() > 0) {
				System.out.println("fetch data:" + this.shareList.pollFirst() + ",thread name:" + Thread.currentThread().getName());
				if (this.shareList.size() <40)
				    notify();
			} else {
				try {
					System.out.println("take等待");
					wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		}
		
		public synchronized void put() {
			if (this.shareList.size() ==100) {
				try {
					System.out.println("put等待");
					wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			} else {
				String data = "" + Math.random();
				System.out.println("put:size:" + this.shareList.size() + "data:" +data +",thread name:" + Thread.currentThread().getName());
				this.shareList.addLast(data);
				notify();
			}
			
		}
	}
	
	static class Consume implements Runnable {
		private Production shareList;
		Consume (Production shareList) {
			this.shareList = shareList;
		}
		public void run() {
			while (true) {
				this.shareList.take();
				try {
					Thread.sleep(200);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			
		}
		
	}
	
	 static class ProviderP implements Runnable{
		private Production shareList;
		ProviderP (Production shareList) {
			this.shareList = shareList;
		}
		public void run() {
			while (true) {
				this.shareList.put();
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
					
			}
			
		}
	}
	
	

}

第二种方式:

package com.jin;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;

public class TakeMultipleThread {
    public static void main(String[] a) {
    	BlockingQueue<String> queue = new LinkedBlockingQueue<String>(); 
		ExecutorService executorService = Executors.newFixedThreadPool(6);
		Consumer consume1 = new Consumer(queue);
		Consumer consume2 = new Consumer(queue);
		Consumer consume3 = new Consumer(queue);
		Consumer consume4 = new Consumer(queue);
		Provider p1 = new Provider(queue);
		Provider p2 = new Provider(queue);
		
		executorService.execute(p1);
		executorService.execute(p2);
		executorService.execute(consume2);
		executorService.execute(consume1);
		//executorService.execute(consume3);
		//executorService.execute(consume4);
	}
    
    private static class Provider implements Runnable{

    	private BlockingQueue<String> blockingQueue;
    	
    	Provider (BlockingQueue<String> blockingQueue) {
    		this.blockingQueue = blockingQueue;
    	}
    	
		public void run() {
			while (true) {
				String random = "" + Math.random();
				this.blockingQueue.offer(random);
				System.out.println("put:size:" + this.blockingQueue.size() + "data:" +random +",thread name:" + Thread.currentThread().getName());
				try {
					Thread.sleep(200);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		}
    	
    }
    
    
    private static class Consumer implements Runnable{

    	private BlockingQueue<String> blockingQueue;
    	
    	Consumer (BlockingQueue<String> blockingQueue) {
    		this.blockingQueue = blockingQueue;
    	}
    	
		public void run() {
			while (true) {
				String random;
				try {
					random = this.blockingQueue.take();
					System.out.println("fetch:size:" + this.blockingQueue.size() + "data:" +random +",thread name:" + Thread.currentThread().getName());
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				try {
					Thread.sleep(230);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			
		}
    	
    }
}
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值