ProduceQueue;支持堵塞和非堵塞
来源:互联网 发布:如何做合格淘宝客服 编辑:程序博客网 时间:2024/06/02 17:38
package com.example.zhanghaohao089.mytest;import java.util.concurrent.TimeUnit;import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantLock;/** * 实现一个消费队列,有堵塞和非堵塞方法 * * @author ZHANGHAOHAO * @date 2017/5/11 */public class ProduceQueue<T> { private T[] items = null; private int head;//头部 private int last;//尾部 private Lock lock = new ReentrantLock(); private Condition notFull = lock.newCondition(); private Condition notEmpty = lock.newCondition(); private int count;//目前的count private int max;//最大内容 public ProduceQueue() { this(10); } public ProduceQueue(int max) { items = (T[])new Object[max]; this.max = max; } //堵塞的 public void put(T item) { lock.lock(); try { if (count == max) { notFull.await(); } enqueue(item); notEmpty.signalAll(); count++; }catch (Exception e) { e.printStackTrace(); }finally { lock.unlock(); } } //非堵塞的 public boolean offer(T item) { lock.lock(); try { if (count == max) { return false; } enqueue(item); notEmpty.signalAll(); count++; } catch (Exception e) { e.printStackTrace(); } finally { lock.unlock(); } return true; } //只堵塞一定时间 public boolean offer(T item, long timeout, TimeUnit unit) { lock.lock(); long nanos = unit.toNanos(timeout); try { if (count == max) { if (nanos <= 0) return false; notFull.awaitNanos(nanos); } enqueue(item); notEmpty.signalAll(); count++; } catch (Exception e) { e.printStackTrace(); }finally { lock.unlock(); } return true; } //非堵塞的 public T poll() { T item = null; lock.lock(); try { if (count == 0) { return null; } item = dequeue(); notFull.signalAll(); count --; } catch (Exception e) { e.printStackTrace(); } finally { lock.unlock(); } return item; } //非堵塞的 public T poll(long timeout, TimeUnit unit) { T item = null; lock.lock(); long nanos = unit.toNanos(timeout); try { if (count == 0) { if (nanos <= 0) return null; notEmpty.awaitNanos(nanos); } item = dequeue(); notFull.signalAll(); count --; } catch (Exception e) { e.printStackTrace(); } finally { lock.unlock(); } return item; } //堵塞的 public T take() { T item = null; lock.lock(); try { if (count == 0) { notEmpty.await(); } item = dequeue(); notFull.signalAll(); count --; } catch (Exception e) { e.printStackTrace(); } finally { lock.unlock(); } return item; } //入队列操作 private void enqueue(T item) { if (last == max) { last = 0; } items[last++] = item; } //出队列操作 private T dequeue() { T item = items[head]; items[head] = null; if (++head == max) { head = 0; } return item; } public int getSize() { return count; }}
0 0
- ProduceQueue;支持堵塞和非堵塞
- 非堵塞
- 同步异步堵塞非堵塞
- c语言 设置堵塞和非堵塞io的方法
- 堵塞与非堵塞IO模式详解
- socket 堵塞非堵塞那些事
- 同步、异步、堵塞、非堵塞和函数调用及I/O之间的组合概念
- 数码管动态扫描(非堵塞)
- read\write 堵塞与非堵塞读取串口数据
- 网络编程基础知识(同步、异步、堵塞、非堵塞)
- read\write 堵塞与非堵塞读取串口数据
- [小代码]非堵塞WEB调用
- 深入flask之异步非堵塞实现
- 深入flask之异步非堵塞实现
- 深入flask之异步非堵塞实现
- serversocket 堵塞
- 线程堵塞
- 堵塞·
- Android中判断是否有网络连接以及网络监控状态
- elasticsearch笔记_精确值搜索_filter(四)
- Python 2.7与Python3 的print有什么区别?
- 【OpenCV3】基于双目视觉的三维重建
- C# System.NullReferenceException: 未将对象引用设置到对象的实例
- ProduceQueue;支持堵塞和非堵塞
- 高并发Java 三 Java内存模型和线程安全
- 文件IO编程十一
- sql创建表、删除表、删除字段、增加字段
- iOS开发之蓝牙通讯
- 显卡结构及工作原理详细解读
- SQL Server 数据库的统计查询
- 77. Combinations
- 碰撞检测问题