ArrayBlockingQueueEx extends ArrayBlockingQueue<E> implements
                                                                  NotificationBlockingQueue<E> { @Override public E peek() { queueLock.lock(); try { while (!finishQueueingFlag && size() == 0) { try { notEmpty.await(); } catch (InterruptedException e) { return null; } } return super.peek(); } 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 testPeek002() throws Exception { int capacity = 1; final long waittime = 1000; final DataValueObject obj1 = new DataValueObject("hoge"); final CountDownLatch threadSync = new CountDownLatch(1); final ArrayBlockingQueueEx<DataValueObject> queue = new ArrayBlockingQueueEx<DataValueObject>(capacity); Thread thread01 = new Thread() { @Override public void run() { try { threadSync.await(); sleep(waittime); } catch (InterruptedException e) { e.printStackTrace(); } queue.add(obj1); } }; assertEquals(0, queue.size()); thread01.start(); long timeStart = System.currentTimeMillis(); threadSync.countDown(); DataValueObject objPeek = queue.peek(); long timeEnd = System.currentTimeMillis(); long timeDiff = timeEnd - timeStart; if (timeDiff < (waittime - 50)) { fail(); } assertEquals(obj1.getValue(), objPeek.getValue()); }
@Test public void testPeek004() throws Exception { int capacity = 1; final long sleeptime = 1000; 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(); DataValueObject result = queue.peek(); long timeEnd = System.currentTimeMillis(); assertNull(result); long timeDiff = timeEnd - timeStart; if (timeDiff < (sleeptime - 50)) { throw new RuntimeException("An exception did not occur within a definite period of time."); } } }; thread01.start(); threadSync.await(); TimeUnit.MILLISECONDS.sleep(sleeptime); thread01.interrupt(); thread01.throwErrorOrExceptionIfThrown(); }