工作中接触到了线程池使用到了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() {
@Override
public 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() {
@Override
public 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
取出操作停止 ...当前的栈容量为 0
Exception 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辆,即 不能够做到即时通信