/** * Factory method to create a blocking buffer. * * @param buffer the buffer to decorate, must not be null * @return a new blocking Buffer * @throws IllegalArgumentException if buffer is null */ public static Buffer decorate(Buffer buffer) { return new BlockingBuffer(buffer); }
/** * Returns a synchronized buffer backed by the given buffer that will * block on {@link Buffer#get()} and {@link Buffer#remove()} operations. * If the buffer is empty, then the {@link Buffer#get()} and * {@link Buffer#remove()} operations will block until new elements * are added to the buffer, rather than immediately throwing a * <code>BufferUnderflowException</code>. * * @param buffer the buffer to synchronize, must not be null * @return a blocking buffer backed by that buffer * @throws IllegalArgumentException if the Buffer is null */ public static Buffer blockingBuffer(Buffer buffer) { return BlockingBuffer.decorate(buffer); }
/** * Gets the next value from the buffer, waiting until an object is * added if the buffer is empty. This method uses the default timeout * set in the constructor. * * @throws BufferUnderflowException if an interrupt is received */ public Object get() { synchronized (lock) { while (collection.isEmpty()) { try { if (timeout <= 0) { lock.wait(); } else { return get(timeout); } } catch (InterruptedException e) { PrintWriter out = new PrintWriter(new StringWriter()); e.printStackTrace(out); throw new BufferUnderflowException("Caused by InterruptedException: " + out.toString()); } } return getBuffer().get(); } }
/** * Removes the next value from the buffer, waiting until an object is * added if the buffer is empty. This method uses the default timeout * set in the constructor. * * @throws BufferUnderflowException if an interrupt is received */ public Object remove() { synchronized (lock) { while (collection.isEmpty()) { try { if (timeout <= 0) { lock.wait(); } else { return remove(timeout); } } catch (InterruptedException e) { PrintWriter out = new PrintWriter(new StringWriter()); e.printStackTrace(out); throw new BufferUnderflowException("Caused by InterruptedException: " + out.toString()); } } return getBuffer().remove(); } }
public void testTimeoutGet() { final BlockingBuffer buffer = new BlockingBuffer( new MyBuffer() ); try { buffer.get( 100 ); fail( "Get should have timed out." ); } catch( BufferUnderflowException e ) { } }
/** * Gets the next value from the buffer, waiting until an object is * added for up to the specified timeout value if the buffer is empty. * * @param timeout the timeout value in milliseconds * @throws BufferUnderflowException if an interrupt is received * @throws BufferUnderflowException if the timeout expires * @since Commons Collections 3.2 */ public Object get(final long timeout) { synchronized (lock) { final long expiration = System.currentTimeMillis() + timeout; long timeLeft = expiration - System.currentTimeMillis(); while (timeLeft > 0 && collection.isEmpty()) { try { lock.wait(timeLeft); timeLeft = expiration - System.currentTimeMillis(); } catch(InterruptedException e) { PrintWriter out = new PrintWriter(new StringWriter()); e.printStackTrace(out); throw new BufferUnderflowException("Caused by InterruptedException: " + out.toString()); } } if (collection.isEmpty()) { throw new BufferUnderflowException("Timeout expired"); } return getBuffer().get(); } }
public void testTimeoutRemove() { final BlockingBuffer buffer = new BlockingBuffer( new MyBuffer() ); try { buffer.remove( 100 ); fail( "Get should have timed out." ); } catch( BufferUnderflowException e ) { } }
/** * Removes the next value from the buffer, waiting until an object is * added if the buffer is empty. This method uses the default timeout * set in the constructor. * * @throws BufferUnderflowException if an interrupt is received */ public Object remove() { synchronized (lock) { while (collection.isEmpty()) { try { if (timeout <= 0) { lock.wait(); } else { return remove(timeout); } } catch (InterruptedException e) { PrintWriter out = new PrintWriter(new StringWriter()); e.printStackTrace(out); throw new BufferUnderflowException("Caused by InterruptedException: " + out.toString()); } } return getBuffer().remove(); } }
/** * Removes the next value from the buffer, waiting until an object is * added for up to the specified timeout value if the buffer is empty. * * @param timeout the timeout value in milliseconds * @throws BufferUnderflowException if an interrupt is received * @throws BufferUnderflowException if the timeout expires * @since Commons Collections 3.2 */ public Object remove(final long timeout) { synchronized (lock) { final long expiration = System.currentTimeMillis() + timeout; long timeLeft = expiration - System.currentTimeMillis(); while (timeLeft > 0 && collection.isEmpty()) { try { lock.wait(timeLeft); timeLeft = expiration - System.currentTimeMillis(); } catch(InterruptedException e) { PrintWriter out = new PrintWriter(new StringWriter()); e.printStackTrace(out); throw new BufferUnderflowException("Caused by InterruptedException: " + out.toString()); } } if (collection.isEmpty()) { throw new BufferUnderflowException("Timeout expired"); } return getBuffer().remove(); } }
/** * Returns a synchronized buffer backed by the given buffer that will * block on {@link Buffer#get()} and {@link Buffer#remove()} operations * until <code>timeout</code> expires. If the buffer is empty, then the * {@link Buffer#get()} and {@link Buffer#remove()} operations will block * until new elements are added to the buffer, rather than immediately * throwing a <code>BufferUnderflowException</code>. * * @param buffer the buffer to synchronize, must not be null * @param timeoutMillis the timeout value in milliseconds, zero or less for no timeout * @return a blocking buffer backed by that buffer * @throws IllegalArgumentException if the Buffer is null * @since Commons Collections 3.2 */ public static Buffer blockingBuffer(Buffer buffer, long timeoutMillis) { return BlockingBuffer.decorate(buffer, timeoutMillis); }
/** * Gets the next value from the buffer, waiting until an object is * added if the buffer is empty. This method uses the default timeout * set in the constructor. * * @throws BufferUnderflowException if an interrupt is received */ public Object get() { synchronized (lock) { while (collection.isEmpty()) { try { if (timeout <= 0) { lock.wait(); } else { return get(timeout); } } catch (InterruptedException e) { PrintWriter out = new PrintWriter(new StringWriter()); e.printStackTrace(out); throw new BufferUnderflowException("Caused by InterruptedException: " + out.toString()); } } return getBuffer().get(); } }
/** * Removes the next value from the buffer, waiting until an object is * added if the buffer is empty. This method uses the default timeout * set in the constructor. * * @throws BufferUnderflowException if an interrupt is received */ public Object remove() { synchronized (lock) { while (collection.isEmpty()) { try { if (timeout <= 0) { lock.wait(); } else { return remove(timeout); } } catch (InterruptedException e) { PrintWriter out = new PrintWriter(new StringWriter()); e.printStackTrace(out); throw new BufferUnderflowException("Caused by InterruptedException: " + out.toString()); } } return getBuffer().remove(); } }
/** * Factory method to create a blocking buffer with a timeout value. * * @param buffer the buffer to decorate, must not be null * @param timeoutMillis the timeout value in milliseconds, zero or less for no timeout * @return a new blocking buffer * @throws IllegalArgumentException if the buffer is null * @since Commons Collections 3.2 */ public static Buffer decorate(Buffer buffer, long timeoutMillis) { return new BlockingBuffer(buffer, timeoutMillis); }
/** * Removes the next value from the buffer, waiting until an object is * added for up to the specified timeout value if the buffer is empty. * * @param timeout the timeout value in milliseconds * @throws BufferUnderflowException if an interrupt is received * @throws BufferUnderflowException if the timeout expires * @since Commons Collections 3.2 */ public Object remove(final long timeout) { synchronized (lock) { final long expiration = System.currentTimeMillis() + timeout; long timeLeft = expiration - System.currentTimeMillis(); while (timeLeft > 0 && collection.isEmpty()) { try { lock.wait(timeLeft); timeLeft = expiration - System.currentTimeMillis(); } catch(InterruptedException e) { PrintWriter out = new PrintWriter(new StringWriter()); e.printStackTrace(out); throw new BufferUnderflowException("Caused by InterruptedException: " + out.toString()); } } if (collection.isEmpty()) { throw new BufferUnderflowException("Timeout expired"); } return getBuffer().remove(); } }
/** * Returns a synchronized buffer backed by the given buffer that will * block on {@link Buffer#get()} and {@link Buffer#remove()} operations. * If the buffer is empty, then the {@link Buffer#get()} and * {@link Buffer#remove()} operations will block until new elements * are added to the buffer, rather than immediately throwing a * <code>BufferUnderflowException</code>. * * @param buffer the buffer to synchronize, must not be null * @return a blocking buffer backed by that buffer * @throws IllegalArgumentException if the Buffer is null */ public static Buffer blockingBuffer(Buffer buffer) { return BlockingBuffer.decorate(buffer); }
/** * Gets the next value from the buffer, waiting until an object is * added if the buffer is empty. This method uses the default timeout * set in the constructor. * * @throws BufferUnderflowException if an interrupt is received */ public Object get() { synchronized (lock) { while (collection.isEmpty()) { try { if (timeout <= 0) { lock.wait(); } else { return get(timeout); } } catch (InterruptedException e) { PrintWriter out = new PrintWriter(new StringWriter()); e.printStackTrace(out); throw new BufferUnderflowException("Caused by InterruptedException: " + out.toString()); } } return getBuffer().get(); } }
/** * Removes the next value from the buffer, waiting until an object is * added if the buffer is empty. This method uses the default timeout * set in the constructor. * * @throws BufferUnderflowException if an interrupt is received */ public Object remove() { synchronized (lock) { while (collection.isEmpty()) { try { if (timeout <= 0) { lock.wait(); } else { return remove(timeout); } } catch (InterruptedException e) { PrintWriter out = new PrintWriter(new StringWriter()); e.printStackTrace(out); throw new BufferUnderflowException("Caused by InterruptedException: " + out.toString()); } } return getBuffer().remove(); } }
/** * Factory method to create a blocking buffer. * * @param buffer the buffer to decorate, must not be null * @return a new blocking Buffer * @throws IllegalArgumentException if buffer is null */ public static Buffer decorate(Buffer buffer) { return new BlockingBuffer(buffer); }
/** * Gets the next value from the buffer, waiting until an object is * added for up to the specified timeout value if the buffer is empty. * * @param timeout the timeout value in milliseconds * @throws BufferUnderflowException if an interrupt is received * @throws BufferUnderflowException if the timeout expires * @since Commons Collections 3.2 */ public Object get(final long timeout) { synchronized (lock) { final long expiration = System.currentTimeMillis() + timeout; long timeLeft = expiration - System.currentTimeMillis(); while (timeLeft > 0 && collection.isEmpty()) { try { lock.wait(timeLeft); timeLeft = expiration - System.currentTimeMillis(); } catch(InterruptedException e) { PrintWriter out = new PrintWriter(new StringWriter()); e.printStackTrace(out); throw new BufferUnderflowException("Caused by InterruptedException: " + out.toString()); } } if (collection.isEmpty()) { throw new BufferUnderflowException("Timeout expired"); } return getBuffer().get(); } }
/** * Returns a synchronized buffer backed by the given buffer that will * block on {@link Buffer#get()} and {@link Buffer#remove()} operations * until <code>timeout</code> expires. If the buffer is empty, then the * {@link Buffer#get()} and {@link Buffer#remove()} operations will block * until new elements are added to the buffer, rather than immediately * throwing a <code>BufferUnderflowException</code>. * * @param buffer the buffer to synchronize, must not be null * @param timeoutMillis the timeout value in milliseconds, zero or less for no timeout * @return a blocking buffer backed by that buffer * @throws IllegalArgumentException if the Buffer is null * @since Commons Collections 3.2 */ public static Buffer blockingBuffer(Buffer buffer, long timeoutMillis) { return BlockingBuffer.decorate(buffer, timeoutMillis); }