ArrayBlockingQueueEx extends ArrayBlockingQueue<E> implements
                                                                  NotificationBlockingQueue<E> { @Override public boolean offer(E o, long timeout, TimeUnit unit) throws InterruptedException { if (o == null) { throw new NullPointerException(); } long nanos = unit.toNanos(timeout); queueLock.lockInterruptibly(); try { while (size() == capacity) { nanos = notFull.awaitNanos(nanos); if (nanos <= 0) { return false; } } boolean success = super.offer(o); if (success) { notEmpty.signal(); } return success; } 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 testOffer001() { int capacity = 3; long timeout = 15; TimeUnit unit = TimeUnit.SECONDS; ArrayBlockingQueueEx<DataValueObject> queue = new ArrayBlockingQueueEx<DataValueObject>(capacity); try { queue.offer(null, timeout, unit); fail(); } catch (NullPointerException e) { } catch (Exception ex) { fail(); } }
@Test public void testOffer002() throws Exception { int capacity = 1; final long timeout = 20000; final TimeUnit unit = TimeUnit.MILLISECONDS; DataValueObject obj1 = new DataValueObject("hoge1"); boolean result1 = false; final CountDownLatch threadSync = new CountDownLatch(1); final ArrayBlockingQueueEx<DataValueObject> queue = new ArrayBlockingQueueEx<DataValueObject>(capacity); result1 = queue.offer(obj1, timeout, unit); assertTrue(result1); ErrorFeedBackThread thread01 = new ErrorFeedBackThread() { @Override public void doRun() throws Exception { DataValueObject obj2 = new DataValueObject("hoge2"); long timeStart = System.currentTimeMillis(); threadSync.countDown(); try { queue.offer(obj2, 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 testOffer006() throws Exception { int capacity = 1; ArrayBlockingQueueEx<DataValueObject> queue = new ArrayBlockingQueueEx<DataValueObject>(capacity); try { queue.offer(null); fail(); } catch (NullPointerException e) { } catch (Exception e) { fail(); } }
@Test public void testOffer008() throws Exception { int capacity = 1; DataValueObject obj1 = new DataValueObject("hoge"); DataValueObject obj2 = new DataValueObject("fuga"); boolean result2 = false; ArrayBlockingQueueEx<DataValueObject> queue = new ArrayBlockingQueueEx<DataValueObject>(capacity); queue.offer(obj1); result2 = queue.offer(obj2); assertFalse(result2); }