@Test public void testRequestExceedPoolCapacity() throws Exception { final long capacity = 1024; final long maxBlockTimeInMs = 200; SimpleByteBufferPool pool = new SimpleByteBufferPool(capacity); try { pool.allocate((int) capacity + 1, maxBlockTimeInMs); fail("Should have thrown!"); } catch (IllegalArgumentException e) { } }
@Override public ByteBuffer allocate(int size, final long timeToBlockInMs) throws TimeoutException, InterruptedException { if (size > capacity) { throw new IllegalArgumentException("Requested size cannot exceed pool capacity."); } else if (timeToBlockInMs < 0) { throw new IllegalArgumentException("timeToBlockInMs cannot be negative."); } final long startTimeInMs = System.currentTimeMillis(); synchronized (lock) { while (size > availableMemory) { long timeout = timeToBlockInMs - (System.currentTimeMillis() - startTimeInMs); if (timeout <= 0) { throw new TimeoutException("Timed out waiting for allocation."); } lock.wait(timeout); } availableMemory -= size; if (availableMemory > 0) { lock.notify(); } } return ByteBuffer.allocate(size); }
SimpleByteBufferPool implements ByteBufferPool { @Override public ByteBuffer allocate(int size, final long timeToBlockInMs) throws TimeoutException, InterruptedException { if (size > capacity) { throw new IllegalArgumentException("Requested size cannot exceed pool capacity."); } else if (timeToBlockInMs < 0) { throw new IllegalArgumentException("timeToBlockInMs cannot be negative."); } final long startTimeInMs = System.currentTimeMillis(); synchronized (lock) { while (size > availableMemory) { long timeout = timeToBlockInMs - (System.currentTimeMillis() - startTimeInMs); if (timeout <= 0) { throw new TimeoutException("Timed out waiting for allocation."); } lock.wait(timeout); } availableMemory -= size; if (availableMemory > 0) { lock.notify(); } } return ByteBuffer.allocate(size); } }
SimpleByteBufferPool implements ByteBufferPool { @Override public ByteBuffer allocate(int size, final long timeToBlockInMs) throws TimeoutException, InterruptedException { if (size > capacity) { throw new IllegalArgumentException("Requested size cannot exceed pool capacity."); } else if (timeToBlockInMs < 0) { throw new IllegalArgumentException("timeToBlockInMs cannot be negative."); } final long startTimeInMs = System.currentTimeMillis(); synchronized (lock) { while (size > availableMemory) { long timeout = timeToBlockInMs - (System.currentTimeMillis() - startTimeInMs); if (timeout <= 0) { throw new TimeoutException("Timed out waiting for allocation."); } lock.wait(timeout); } availableMemory -= size; if (availableMemory > 0) { lock.notify(); } } return ByteBuffer.allocate(size); } SimpleByteBufferPool(long capacity); }
SimpleByteBufferPool implements ByteBufferPool { @Override public ByteBuffer allocate(int size, final long timeToBlockInMs) throws TimeoutException, InterruptedException { if (size > capacity) { throw new IllegalArgumentException("Requested size cannot exceed pool capacity."); } else if (timeToBlockInMs < 0) { throw new IllegalArgumentException("timeToBlockInMs cannot be negative."); } final long startTimeInMs = System.currentTimeMillis(); synchronized (lock) { while (size > availableMemory) { long timeout = timeToBlockInMs - (System.currentTimeMillis() - startTimeInMs); if (timeout <= 0) { throw new TimeoutException("Timed out waiting for allocation."); } lock.wait(timeout); } availableMemory -= size; if (availableMemory > 0) { lock.notify(); } } return ByteBuffer.allocate(size); } SimpleByteBufferPool(long capacity); @Override ByteBuffer allocate(int size, final long timeToBlockInMs); @Override void deallocate(ByteBuffer buffer); long availableMemory(); long capacity(); }
SimpleByteBufferPool implements ByteBufferPool { @Override public ByteBuffer allocate(int size, final long timeToBlockInMs) throws TimeoutException, InterruptedException { if (size > capacity) { throw new IllegalArgumentException("Requested size cannot exceed pool capacity."); } else if (timeToBlockInMs < 0) { throw new IllegalArgumentException("timeToBlockInMs cannot be negative."); } final long startTimeInMs = System.currentTimeMillis(); synchronized (lock) { while (size > availableMemory) { long timeout = timeToBlockInMs - (System.currentTimeMillis() - startTimeInMs); if (timeout <= 0) { throw new TimeoutException("Timed out waiting for allocation."); } lock.wait(timeout); } availableMemory -= size; if (availableMemory > 0) { lock.notify(); } } return ByteBuffer.allocate(size); } SimpleByteBufferPool(long capacity); @Override ByteBuffer allocate(int size, final long timeToBlockInMs); @Override void deallocate(ByteBuffer buffer); long availableMemory(); long capacity();  }