//阻塞队列 class ArrayBlockingQueue{ int size = 10; Deque queue = new LinkedList(); ReetrantLock lock = new ReetrantLock(); Condition empty = lock.newCondition(), full = lock.newCondition(); Object poll() { lock.lock(); try { while (queue.isEmpty()) { full.await(); } Object res = queue.pollFirst(); empty.signal(); return res; } catch (Exception e) { // } finally { lock.unlock(); } } void offer(Object object) { lock.lock(); try { while (queue.size() == size) { empty.await(); } queue.offer(object); full.signal(); return; } catch (Exception e) { // } finally { lock.unlock(); } } } 阻塞队列的败笔就是,ArrayBlockingQueue我用了LinkedList存储元素
点赞 2

相关推荐

09-01 21:40
已编辑
同济大学 Java
点赞 评论 收藏
分享
点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务