使用Synchronzied和线程通信实现一个BlockingQueue

来源:互联网 发布:手机好莱坞特效软件 编辑:程序博客网 时间:2024/06/05 04:25

工作中接触到了线程池使用到了BlockingQueue,那么如何用基本的线程知识实现BlockingQueue呢

代码如下

package blockingQueue;import java.util.LinkedList;import java.util.concurrent.atomic.AtomicInteger;public class MyBlockingQueue {//volatile和final不能同时使用,volatile表示不同线程可见此变量,意味着可以进行修改,final则指定对象不可变private final LinkedList<Object> list = new LinkedList<Object>();//计数器private AtomicInteger count = new AtomicInteger(0);//最小长度private final int minSize = 0;//最大长度采用构造器注入private final int maxSize;//构造器public MyBlockingQueue(int size) {this.maxSize = size;}//构建对象锁private final Object lock = new Object();public int getSizer(){return this.count.get();}public void put(Object obj){synchronized (lock) {try {//如果等于最大的容量则阻塞while(count.get() == maxSize){System.out.println("存放操作停止 " + "..." + "当前的栈容量为 " + count);lock.wait();}//放入新的元素list.add(obj);//计数器递增count.incrementAndGet();//唤醒在take中阻塞的线程lock.notify();System.out.println("新增的元素为 " + obj + "..." + "当前的栈容量为 " + count);} catch (InterruptedException e) {e.printStackTrace();}}}public Object take(){Object obj = null;synchronized (lock) {try {while(count.get() == minSize){System.out.println("取出操作停止 " + "..." + "当前的栈容量为 " + count);lock.wait();}//取出元素obj = list.removeFirst();//计数器递减count.decrementAndGet();//唤醒在put中阻塞的线程lock.notify();System.out.println("移除的元素为 " + obj + "..." + "当前的栈容量为 " + count);return obj;} catch (Exception e) {e.printStackTrace();}}return obj;}public static void main(String[] args) {final MyBlockingQueue blockingQueue = new MyBlockingQueue(5);blockingQueue.put("本田");blockingQueue.put("奔驰");blockingQueue.put("别克");blockingQueue.put("宝马");blockingQueue.put("三菱");Thread thread1 = new Thread(new Runnable() {@Overridepublic void run() {int count = 2014;while(true){blockingQueue.put("本田" + count +"型");count++;if(count == 20001){throw new RuntimeException("2w辆车后停产");}}}});thread1.start();Thread thread2 = new Thread(new Runnable() {@Overridepublic void run() {while(true){blockingQueue.take();}}});thread2.start();}}
执行的结果

新增的元素为 本田19995型...当前的栈容量为 3新增的元素为 本田19996型...当前的栈容量为 4新增的元素为 本田19997型...当前的栈容量为 5存放操作停止 ...当前的栈容量为 5移除的元素为 本田19993型...当前的栈容量为 4移除的元素为 本田19994型...当前的栈容量为 3移除的元素为 本田19995型...当前的栈容量为 2新增的元素为 本田19998型...当前的栈容量为 3新增的元素为 本田19999型...当前的栈容量为 4新增的元素为 本田20000型...当前的栈容量为 5移除的元素为 本田19996型...当前的栈容量为 4移除的元素为 本田19997型...当前的栈容量为 3移除的元素为 本田19998型...当前的栈容量为 2移除的元素为 本田19999型...当前的栈容量为 1移除的元素为 本田20000型...当前的栈容量为 0取出操作停止 ...当前的栈容量为 0Exception in thread "Thread-0" java.lang.RuntimeException: 2w辆车后停产at blockingQueue.MyBlockingQueue$1.run(MyBlockingQueue.java:87)at java.lang.Thread.run(Thread.java:745)
出现的问题:只能生产完5辆然后消费5辆,即 不能够做到即时通信


0 0
原创粉丝点击