自定义一个简单的阻塞队列
2017-08-29 15:44
281 查看
通过LinkedList实现一个简单的阻塞队列,实现put和get方法
public class MyBlockingQueue<T> {
private LinkedList<T> linkedList = new LinkedList<>();
private AtomicInteger count = new AtomicInteger(0);
private int minSize = 0;
private int maxSize;
private Object lock = new Object();
public MyBlockingQueue(int maxSize){
this.maxSize = maxSize;
}
public void put(T t){
synchronized (lock){
while(count.get() == maxSize){
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
linkedList.add(t);
count.incrementAndGet();
System.out.println(" 元素 " + t + " 被添加 ");
lock.notify();
}
}
public T take(){
T temp;
synchronized (lock){
while (count.get() == minSize){
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
temp = linkedList.removeFirst();
count.decrementAndGet();
System.out.println(" 元素 " + temp + " 被消费 ");
lock.notify();
}
return temp;
}
public int getSize(){
return count.get();
}
public static void main(String[] args) {
MyBlockingQueue<String> queue = new MyBlockingQueue<String>(5);
queue.put("a");
queue.put("b");
queue.put("c");
queue.put("d");
queue.put("e");
new Thread(new Runnable() {
@Override
public void run() {
queue.put("+1");
queue.put("+2");
}
}).start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
queue.take();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
queue.take();
}
}).start();
}
}
public class MyBlockingQueue<T> {
private LinkedList<T> linkedList = new LinkedList<>();
private AtomicInteger count = new AtomicInteger(0);
private int minSize = 0;
private int maxSize;
private Object lock = new Object();
public MyBlockingQueue(int maxSize){
this.maxSize = maxSize;
}
public void put(T t){
synchronized (lock){
while(count.get() == maxSize){
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
linkedList.add(t);
count.incrementAndGet();
System.out.println(" 元素 " + t + " 被添加 ");
lock.notify();
}
}
public T take(){
T temp;
synchronized (lock){
while (count.get() == minSize){
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
temp = linkedList.removeFirst();
count.decrementAndGet();
System.out.println(" 元素 " + temp + " 被消费 ");
lock.notify();
}
return temp;
}
public int getSize(){
return count.get();
}
public static void main(String[] args) {
MyBlockingQueue<String> queue = new MyBlockingQueue<String>(5);
queue.put("a");
queue.put("b");
queue.put("c");
queue.put("d");
queue.put("e");
new Thread(new Runnable() {
@Override
public void run() {
queue.put("+1");
queue.put("+2");
}
}).start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
queue.take();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
queue.take();
}
}).start();
}
}
相关文章推荐
- 【JAVA】简单实现一个阻塞任务队列
- 一个简单的阻塞队列实现
- 自己实现一个简单的优先队列-二叉堆
- ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布局
- 千万级数据分页之二---一个简单的自定义分页控件
- [原创][Java]一个简单高效的线程安全队列的JAVA实现
- 使用CoreGraphics绘制一个简单的折线图,可自定义坐标。
- Android AsyncTask 深度理解、简单封装、任务队列分析、自定义线程池
- Groovy探索 自定义Range 一 一个简单的自定义Range类
- 实现一个简单的无界队列锁
- sharepoint 2010 创建一个简单的列表自定义字段
- 一个简单的自定义ClassLoader的实现
- 一个简单的带Body的自定义Tag开发过程
- 一个简单的自定义通信协议(socket)
- kotlin学习之自定义lambda表达式----简单易懂教你写一个可以接受lambda表达式的函数
- ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布局
- ios开发UI基础—使用纯代码自定义UItableviewcell实现一个简单的微博界面布局
- 每个锁对象都有两个队列,一个是就绪队列,一个是阻塞队列
- 一个简单的队列