ConcurrentLinkedQueue 以及 BlockingQueue

import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import com.google.common.collect.Queues;


public class ConcurrentLinkedQueueTest {
	private static ConcurrentLinkedQueue<Integer> queue = Queues.newConcurrentLinkedQueue();
	private static int count = 2;//线程个数
	/*
	 * CountDownLatch,同步辅助类, 
	 */
	private static CountDownLatch latch = new CountDownLatch(count);
	/*
	 * 生产
	 */
	public static void offer(){
		for(int i = 0; i < 100000; i++){
			queue.offer(i);
		}
	}
	/**
	 * 消费
	 */
	static class Poll implements Runnable{
		@Override
		public void run() {
			 while (!queue.isEmpty()) {
	                System.out.println(queue.poll());
	            }
			 latch.countDown();
		}
	}
	public static void main(String[] args) throws InterruptedException {
		long timeStart = System.currentTimeMillis();
        ExecutorService es = Executors.newFixedThreadPool(4);
        ConcurrentLinkedQueueTest.offer();
        for (int i = 0; i < count; i++) {//count = 2
            es.submit(new Poll());
        }
        latch.await(); //使得主线程(main)阻塞直到latch.countDown()为零才继续执行
        System.out.println("cost time " + (System.currentTimeMillis() - timeStart) + "ms");
        es.shutdown();
	}
}



import com.google.common.collect.Queues;

public class BlockingQueueTest {
	
	public class Basket{
		BlockingQueue<String> basket = Queues.newLinkedBlockingQueue(3);
		public void produce() throws InterruptedException{
			basket.put("an apple");
		}
		public String consume() throws InterruptedException{
			return basket.take();
		}
	}
	class Producer implements Runnable {
		private String instance;
		private Basket basket;
		public Producer(String instance, Basket basket){
			this.instance = instance;
			this.basket = basket;
		}
		@Override
		public void run() {
			try {
				while(true){
					System.out.println("produce ready:" + instance);
					basket.produce();
					System.out.println("produce end:" + instance);
					Thread.sleep(300);
				}
			} catch (InterruptedException e) {
				System.out.println("produce Exception:" + e.toString());
			}
		}
		
	}
	class Consumer implements Runnable {
		private String instance;
		private Basket basket;
		public Consumer(String instance, Basket basket){
			this.instance = instance;
			this.basket = basket;
		}
		@Override
		public void run() {
			try {
				while(true){
					System.out.println("consume ready: " + instance);
					System.out.println(basket.consume());
					System.out.println("consume: end" + instance);
					Thread.sleep(1000);
				}
			} catch (InterruptedException e) {
				System.out.println("consume: Exception" + e.toString());
				e.printStackTrace();
			}
		}
	} 
	public static void main(String[] args) {
		BlockingQueueTest t = new BlockingQueueTest();
		
		Basket basket = t.new Basket();
		ExecutorService service = Executors.newCachedThreadPool();
		Producer producer = t.new Producer("producer 1", basket);
		Producer producer2 = t.new Producer("producer 2", basket);
		Consumer consumer = t.new Consumer("consume 1", basket);
		service.submit(producer);
		service.submit(producer2);
		service.submit(consumer);
		
		try {
			Thread.sleep(1000 * 5);
		} catch (Exception e) {
			e.printStackTrace();
		}
		service.shutdownNow();
//		service.shutdown();
	}
}


你可能感兴趣的:(多线程)