ArrayBlockingQueueEx extends ArrayBlockingQueue<E> implements
                                                                  NotificationBlockingQueue<E> { @Override public E poll(long timeout, TimeUnit unit) throws InterruptedException { long nanos = unit.toNanos(timeout); queueLock.lock(); try { while (!finishQueueingFlag && size() == 0) { nanos = notEmpty.awaitNanos(nanos); if (nanos <= 0) { return null; } } if (finishQueueingFlag && size() == 0) { return null; } E elm = super.poll(timeout, unit); if (elm != null) { notFull.signal(); } return elm; } finally { queueLock.unlock(); } } ArrayBlockingQueueEx(int capacity, boolean fair);  ArrayBlockingQueueEx(int capacity); @Override void finishQueueing(); @Override boolean offer(E o, long timeout, TimeUnit unit); @Override boolean offer(E o); @Override void put(E o); @Override E peek(); @Override E poll(long timeout, TimeUnit unit); @Override E poll(); @Override boolean isEmpty();  }
@Test public void testPoll001() throws Exception { int capacity = 1; final long timeout = 20000; final TimeUnit unit = TimeUnit.MILLISECONDS; final CountDownLatch threadSync = new CountDownLatch(1); final ArrayBlockingQueueEx<DataValueObject> queue = new ArrayBlockingQueueEx<DataValueObject>(capacity); ErrorFeedBackThread thread01 = new ErrorFeedBackThread() { @Override public void doRun() throws Exception { long timeStart = System.currentTimeMillis(); threadSync.countDown(); try { queue.poll(timeout, unit); throw new RuntimeException("An exception did not occur within a definite period of time."); } catch (InterruptedException e) { } long timeEnd = System.currentTimeMillis(); long timeDiff = timeEnd - timeStart; if (timeDiff < 950) { throw new RuntimeException("An exception did not occur within a definite period of time."); } } }; thread01.start(); threadSync.await(); TimeUnit.MILLISECONDS.sleep(1000); thread01.interrupt(); thread01.throwErrorOrExceptionIfThrown(); }
@Test public void testPoll002() throws Exception { int capacity = 1; long timeout = 1000; TimeUnit unit = TimeUnit.MILLISECONDS; ArrayBlockingQueueEx<DataValueObject> queue = new ArrayBlockingQueueEx<DataValueObject>(capacity); assertEquals(0, queue.size()); long timeStart = System.currentTimeMillis(); DataValueObject objResult = queue.poll(timeout, unit); long timeEnd = System.currentTimeMillis(); long timeDiff = timeEnd - timeStart; if (timeDiff < (timeout - 50)) { fail(); } assertNull(objResult); }
@Test public void testPoll004() throws Exception { int capacity = 3; long timeout = 100; TimeUnit unit = TimeUnit.MILLISECONDS; ArrayBlockingQueueEx<DataValueObject> queue = new ArrayBlockingQueueEx<DataValueObject>(capacity); DataValueObject obj1 = new DataValueObject("hoge"); DataValueObject obj2 = new DataValueObject("fuga"); DataValueObject obj3 = new DataValueObject("piyo"); queue.add(obj1); queue.add(obj2); queue.add(obj3); int sizeBefore = queue.size(); DataValueObject objPoll = queue.poll(timeout, unit); int sizeAfter = queue.size(); assertEquals(obj1, objPoll); assertEquals(1, sizeBefore - sizeAfter); }