public BufferPool() { pool = new Queue[27]; for (int i=0; i<pool.length; i++) { /** @todo try various queue implementations */ pool[i] = new LinkedBlockingQueue(); } }
/** * Creates a <tt>LinkedBlockingQueue</tt> with a capacity of * {@link Integer#MAX_VALUE}, initially containing the elements of the * given collection, * added in traversal order of the collection's iterator. * * @param c the collection of elements to initially contain * @throws NullPointerException if the specified collection or any * of its elements are null */ public LinkedBlockingQueue(Collection c) { this(Integer.MAX_VALUE); for (Iterator itr = c.iterator(); itr.hasNext();) { Object e = itr.next(); add(e); } }
public Object poll() { if (count == 0) return null; Object x = null; int c = -1; synchronized (takeLock) { if (count > 0) { x = extract(); synchronized (this) { c = count--; } if (c > 1) takeLock.notify(); } } if (c == capacity) signalNotFull(); return x; }
/** * Inserts the specified element at the tail of this queue if it is * possible to do so immediately without exceeding the queue's capacity, * returning <tt>true</tt> upon success and <tt>false</tt> if this queue * is full. * When using a capacity-restricted queue, this method is generally * preferable to method {@link BlockingQueue#add add}, which can fail to * insert an element only by throwing an exception. * * @throws NullPointerException if the specified element is null */ public boolean offer(Object e) { if (e == null) throw new NullPointerException(); if (count == capacity) return false; int c = -1; synchronized (putLock) { if (count < capacity) { insert(e); synchronized (this) { c = count++; } if (c + 1 < capacity) putLock.notify(); } } if (c == 0) signalNotEmpty(); return c >= 0; }
/** * Inserts the specified element at the tail of this queue if it is * possible to do so immediately without exceeding the queue's capacity, * returning <tt>true</tt> upon success and <tt>false</tt> if this queue * is full. * When using a capacity-restricted queue, this method is generally * preferable to method {@link BlockingQueue#add add}, which can fail to * insert an element only by throwing an exception. * * @throws NullPointerException if the specified element is null */ public boolean offer(Object e) { if (e == null) throw new NullPointerException(); if (count == capacity) return false; int c = -1; synchronized (putLock) { if (count < capacity) { insert(e); synchronized (this) { c = count++; } if (c + 1 < capacity) putLock.notify(); } } if (c == 0) signalNotEmpty(); return c >= 0; }
/** * Creates an ExecutorCompletionService using the supplied * executor for base task execution and a * {@link LinkedBlockingQueue} as a completion queue. * * @param executor the executor to use * @throws NullPointerException if executor is <tt>null</tt> */ public ExecutorCompletionService(Executor executor) { if (executor == null) throw new NullPointerException(); this.executor = executor; this.aes = (executor instanceof AbstractExecutorService) ? (AbstractExecutorService) executor : null; this.completionQueue = new LinkedBlockingQueue(); }
public Object poll() { if (count == 0) return null; Object x = null; int c = -1; synchronized (takeLock) { if (count > 0) { x = extract(); synchronized (this) { c = count--; } if (c > 1) takeLock.notify(); } } if (c == capacity) signalNotFull(); return x; }
/** * Inserts the specified element at the tail of this queue if it is * possible to do so immediately without exceeding the queue's capacity, * returning <tt>true</tt> upon success and <tt>false</tt> if this queue * is full. * When using a capacity-restricted queue, this method is generally * preferable to method {@link BlockingQueue#add add}, which can fail to * insert an element only by throwing an exception. * * @throws NullPointerException if the specified element is null */ public boolean offer(Object e) { if (e == null) throw new NullPointerException(); if (count == capacity) return false; int c = -1; synchronized (putLock) { if (count < capacity) { insert(e); synchronized (this) { c = count++; } if (c + 1 < capacity) putLock.notify(); } } if (c == 0) signalNotEmpty(); return c >= 0; }
/** * Creates a <tt>LinkedBlockingQueue</tt> with a capacity of * {@link Integer#MAX_VALUE}, initially containing the elements of the * given collection, * added in traversal order of the collection's iterator. * * @param c the collection of elements to initially contain * @throws NullPointerException if the specified collection or any * of its elements are null */ public LinkedBlockingQueue(Collection c) { this(Integer.MAX_VALUE); for (Iterator itr = c.iterator(); itr.hasNext();) { Object e = itr.next(); add(e); } }
/** * Creates an ExecutorCompletionService using the supplied * executor for base task execution and a * {@link LinkedBlockingQueue} as a completion queue. * * @param executor the executor to use * @throws NullPointerException if executor is <tt>null</tt> */ public ExecutorCompletionService(Executor executor) { if (executor == null) throw new NullPointerException(); this.executor = executor; this.aes = (executor instanceof AbstractExecutorService) ? (AbstractExecutorService) executor : null; this.completionQueue = new LinkedBlockingQueue(); }
public Object poll() { if (count == 0) return null; Object x = null; int c = -1; synchronized (takeLock) { if (count > 0) { x = extract(); synchronized (this) { c = count--; } if (c > 1) takeLock.notify(); } } if (c == capacity) signalNotFull(); return x; }
insert(e); synchronized (this) { c = count++; } if (c + 1 < capacity) signalNotEmpty();
/** * Creates a <tt>LinkedBlockingQueue</tt> with a capacity of * {@link Integer#MAX_VALUE}, initially containing the elements of the * given collection, * added in traversal order of the collection's iterator. * * @param c the collection of elements to initially contain * @throws NullPointerException if the specified collection or any * of its elements are null */ public LinkedBlockingQueue(Collection c) { this(Integer.MAX_VALUE); for (Iterator itr = c.iterator(); itr.hasNext();) { Object e = itr.next(); add(e); } }
/** * @return Gets the executor service. This is not publically accessible. */ ThreadPoolExecutor getExecutorService() { if (executorService == null) { synchronized (this) { executorService = new ThreadPoolExecutor(EXECUTOR_CORE_POOL_SIZE, EXECUTOR_MAXIMUM_POOL_SIZE, EXECUTOR_KEEP_ALIVE_TIME, TimeUnit.MILLISECONDS, new LinkedBlockingQueue()); } } return executorService; }
public Object take() throws InterruptedException { Object x; int c = -1; synchronized (takeLock) { try { while (count == 0) takeLock.wait(); } catch (InterruptedException ie) { takeLock.notify(); // propagate to a non-interrupted thread throw ie; } x = extract(); synchronized (this) { c = count--; } if (c > 1) takeLock.notify(); } if (c == capacity) signalNotFull(); return x; }
insert(e); synchronized (this) { c = count++; } if (c + 1 < capacity) signalNotEmpty();
/** * Reconstitute this queue instance from a stream (that is, * deserialize it). * @param s the stream */ private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException { // Read in capacity, and any hidden stuff s.defaultReadObject(); synchronized (this) { count = 0; } last = head = new Node(null); // Read in all elements and place in queue for (;;) { Object item = (Object)s.readObject(); if (item == null) break; add(item); } }
/** * @return Gets the executor service. This is not publically accessible. */ ThreadPoolExecutor getExecutorService() { if (executorService == null) { synchronized (this) { executorService = new ThreadPoolExecutor(EXECUTOR_CORE_POOL_SIZE, EXECUTOR_MAXIMUM_POOL_SIZE, EXECUTOR_KEEP_ALIVE_TIME, TimeUnit.MILLISECONDS, new LinkedBlockingQueue()); } } return executorService; }
public Object take() throws InterruptedException { Object x; int c = -1; synchronized (takeLock) { try { while (count == 0) takeLock.wait(); } catch (InterruptedException ie) { takeLock.notify(); // propagate to a non-interrupted thread throw ie; } x = extract(); synchronized (this) { c = count--; } if (c > 1) takeLock.notify(); } if (c == capacity) signalNotFull(); return x; }
insert(e); synchronized (this) { c = count++; } if (c + 1 < capacity) signalNotEmpty();