/** * Factory method to create a bounded buffer that blocks for a maximum * amount of time. * * @param buffer the buffer to decorate, must not be null * @param maximumSize the maximum size, must be size one or greater * @param timeout the maximum amount of time to wait in milliseconds * @return a new bounded buffer * @throws IllegalArgumentException if the buffer is null * @throws IllegalArgumentException if the maximum size is zero or less */ public static BoundedBuffer decorate(Buffer buffer, int maximumSize, long timeout) { return new BoundedBuffer(buffer, maximumSize, timeout); }
/** * Returns a synchronized buffer backed by the given buffer that will * block on {@link Buffer#add(Object)} and * {@link Buffer#addAll(java.util.Collection)} until enough object(s) are * removed from the buffer to allow the object(s) to be added and still * maintain the maximum size. * * @param buffer the buffer to make bounded, must not be null * @param maximumSize the maximum size * @return a bounded buffer backed by the given buffer * @throws IllegalArgumentException if the given buffer is null * @since Commons Collections 3.2 */ public static Buffer boundedBuffer(Buffer buffer, int maximumSize) { return BoundedBuffer.decorate(buffer, maximumSize); }
public boolean add(Object o) { synchronized (lock) { timeoutWait(1); return getBuffer().add(o); } }
public Object remove() { synchronized (lock) { Object returnValue = getBuffer().remove(); lock.notifyAll(); return returnValue; } }
BoundedBuffer boundedBuffer = new BoundedBuffer(); boundedBuffer.put("test"); .......
final BoundedBuffer bf=new BoundedBuffer(); try { while(true) bf.add(new Object()); } catch (InterruptedException e) { try { while(true) bf.remove(); } catch (InterruptedException e) {
public Object remove() { synchronized (lock) { Object returnValue = getBuffer().remove(); lock.notifyAll(); return returnValue; } }
public boolean addAll(final Collection c) { synchronized (lock) { timeoutWait(c.size()); return getBuffer().addAll(c); } }
/** * Returns a synchronized buffer backed by the given buffer that will * block on {@link Buffer#add(Object)} and * {@link Buffer#addAll(java.util.Collection)} until enough object(s) are * removed from the buffer to allow the object(s) to be added and still * maintain the maximum size or the timeout expires. * * @param buffer the buffer to make bounded, must not be null * @param maximumSize the maximum size * @param timeoutMillis the timeout value in milliseconds, zero or less for no timeout * @return a bounded buffer backed by the given buffer * @throws IllegalArgumentException if the given buffer is null * @since Commons Collections 3.2 */ public static Buffer boundedBuffer(Buffer buffer, int maximumSize, long timeoutMillis) { return BoundedBuffer.decorate(buffer, maximumSize, timeoutMillis); }
/** * Factory method to create a bounded buffer. * <p> * When the buffer is full, it will immediately throw a * <code>BufferOverflowException</code> on calling <code>add()</code>. * * @param buffer the buffer to decorate, must not be null * @param maximumSize the maximum size, must be size one or greater * @return a new bounded buffer * @throws IllegalArgumentException if the buffer is null * @throws IllegalArgumentException if the maximum size is zero or less */ public static BoundedBuffer decorate(Buffer buffer, int maximumSize) { return new BoundedBuffer(buffer, maximumSize, 0L); }
if (getBuffer().size() + nAdditions > maximumSize) { throw new BufferOverflowException( "Buffer size cannot exceed " + maximumSize); while (timeLeft > 0 && getBuffer().size() + nAdditions > maximumSize) { try { lock.wait(timeLeft); if (getBuffer().size() + nAdditions > maximumSize) { throw new BufferOverflowException("Timeout expired");
public boolean add(Object o) { synchronized (lock) { timeoutWait(1); return getBuffer().add(o); } }
/** * Returns a synchronized buffer backed by the given buffer that will * block on {@link Buffer#add(Object)} and * {@link Buffer#addAll(java.util.Collection)} until enough object(s) are * removed from the buffer to allow the object(s) to be added and still * maintain the maximum size or the timeout expires. * * @param buffer the buffer to make bounded, must not be null * @param maximumSize the maximum size * @param timeoutMillis the timeout value in milliseconds, zero or less for no timeout * @return a bounded buffer backed by the given buffer * @throws IllegalArgumentException if the given buffer is null * @since Commons Collections 3.2 */ public static Buffer boundedBuffer(Buffer buffer, int maximumSize, long timeoutMillis) { return BoundedBuffer.decorate(buffer, maximumSize, timeoutMillis); }
/** * Factory method to create a bounded buffer that blocks for a maximum * amount of time. * * @param buffer the buffer to decorate, must not be null * @param maximumSize the maximum size, must be size one or greater * @param timeout the maximum amount of time to wait in milliseconds * @return a new bounded buffer * @throws IllegalArgumentException if the buffer is null * @throws IllegalArgumentException if the maximum size is zero or less */ public static BoundedBuffer decorate(Buffer buffer, int maximumSize, long timeout) { return new BoundedBuffer(buffer, maximumSize, timeout); }
if (getBuffer().size() + nAdditions > maximumSize) { throw new BufferOverflowException( "Buffer size cannot exceed " + maximumSize); while (timeLeft > 0 && getBuffer().size() + nAdditions > maximumSize) { try { lock.wait(timeLeft); if (getBuffer().size() + nAdditions > maximumSize) { throw new BufferOverflowException("Timeout expired");
public boolean addAll(final Collection c) { synchronized (lock) { timeoutWait(c.size()); return getBuffer().addAll(c); } }