/** * 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; }
/** * 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; }
signalNotEmpty(); return true;