2017-10-26 消息队列设计
一:利用阻塞队列:
Producer:
package ; import java.util.Random; import java.util.concurrent.BlockingQueue; import java.util.concurrent.atomic.AtomicInteger; public class Producer implements Runnable { private BlockingQueue<PCData> queue;// 内存缓冲区 private static AtomicInteger count = new AtomicInteger();// 总数 原子操作 public Producer(BlockingQueue<PCData> queue) { this.queue = queue; } @Override public void run() { PCData data = null; Random r = new Random(); try { while (true) { Thread.sleep(r.nextInt(1000)); data = new PCData(count.incrementAndGet()); System.out.println(data + " 加入队列" ); if (!queue.offer(data)) { System.err.println(" 加入队列失败"); } } } catch (InterruptedException e) { e.printStackTrace(); Thread.currentThread().interrupt(); } } }
Consumer:
import java.util.Random; import java.util.concurrent.BlockingQueue; public class Consumer implements Runnable { private BlockingQueue<PCData> queue; private static final int SLEEPTIME = 1000; public Consumer(BlockingQueue<PCData> queue) { this.queue = queue; } @Override public void run() { System.out.println("start Consumer id :" + Thread.currentThread().getId()); Random r = new Random(); try { while (true) { PCData data = queue.take(); if (data != null) { System.out.println("消费了:" + data); Thread.sleep(r.nextInt(SLEEPTIME)); } } } catch (InterruptedException e) { e.printStackTrace(); Thread.currentThread().interrupt(); } } }
PCData:
public class PCData { private final int intData; public PCData(int d) { intData = d; } public PCData(String d) { intData = Integer.valueOf(d); } public int getData() { return intData; } @Override public String toString() { return "data:" + intData; } }
ProducterAndConsumer:
import java.util.concurrent.BlockingQueue; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.LinkedBlockingDeque; public class ProducterAndConsumer { public static void main(String[] args) throws InterruptedException { BlockingQueue<PCData> queue = new LinkedBlockingDeque<>(5); Producer p1 = new Producer(queue); Producer p2 = new Producer(queue); Producer p3 = new Producer(queue); Consumer c1 = new Consumer(queue); //Consumer c2 = new Consumer(queue); // Consumer c3 = new Consumer(queue); ExecutorService service = Executors.newCachedThreadPool(); service.execute(p1); service.execute(p2); service.execute(p3); service.execute(c1); //service.execute(c2); // service.execute(c3); } }
二:利用wait/notify:
Producer:
package 生产者消费者模式; import java.util.List; import java.util.Random; public class Producer implements Runnable { private List<PCData> queue; private int length; public Producer(List<PCData> queue, int length) { this.queue = queue; this.length = length; } public void run() { try { while(true) { if (Thread.currentThread().isInterrupted()) //无限判断线程状态,中断了就返回true break; Random r = new Random(); long temp = r.nextInt(100); System.out.println(Thread.currentThread().getId() + "生产了:" + temp + "缓存大小:" + queue.size()); PCData data = new PCData(); data.set(temp); synchronized (queue) { if (queue.size() >= length) { //System.out.println("满啦~~~,无法放入"); queue.notifyAll(); queue.wait(); } else { queue.add(data); } Thread.sleep(2000); } } } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
Consumer:
package 生产者消费者模式; import java.util.List; public class Consumer implements Runnable { private List<PCData> queue; public Consumer(List<PCData> queue) { this.queue = queue; } public void run() { try { while(true) { if(Thread.currentThread().isInterrupted()) break; PCData data = null; synchronized (queue) { if (queue.size() == 0) { //System.out.println("空啦~~~~,无法获取"); queue.wait(); queue.notifyAll(); } data = queue.remove(0); } System.out.println(Thread.currentThread().getId()+"消费了" + data.get() +"缓存大小:" + queue.size()); Thread.sleep(1000); } } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
PCData:
package 生产者消费者模式; public class PCData { private long value; public void set(long value) { this.value = value; } public long get(){ return value; } }
ProducterAndConsumer:
package 生产者消费者模式; import java.util.ArrayList; import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class ProducterAndConsumer { public static void main(String[] args) { List<PCData> queue = new ArrayList<>(); int length = 10; Producer p1 = new Producer(queue, length); //Producer p2 = new Producer(queue, length); Consumer c1 = new Consumer(queue); Consumer c2 = new Consumer(queue); ExecutorService service = Executors.newCachedThreadPool(); service.execute(c1); service.execute(p1); //service.execute(p2); service.execute(c2); } }