/** * Same as {@code monitor.enterWhen(guard)} but copes with that pesky {@code * InterruptedException} by catching it and setting this thread's * interrupted flag. */ private static boolean safeEnterWhen(Monitor monitor, Guard guard) { try { monitor.enterWhen(guard); return true; } catch (InterruptedException ignore) { currentThread().interrupt(); return false; } }
public void addToList(String item) throws InterruptedException { monitor.enterWhen(listBelowCapacity); try { list.add(item); } finally { monitor.leave(); } } }
public void demoEnterWhen() throws InterruptedException { monitor.enterWhen(conditionGuard); try { taskDoneCounter++; if (taskDoneCounter == stopTaskCount) { condition = false; } } finally { monitor.leave(); } }
/** * See {@link java.util.concurrent.BlockingQueue#take}, it also waits for any updating * operations to finish. */ @Override public Runnable take() throws InterruptedException { //int num=this.takes.incrementAndGet(); //System.out.println("Take("+num+"): before mon"); monitor.enterWhen(canTake); //System.out.println("Take("+num+"): entered monitor"); Runnable res=this.delegate.poll(); monitor.leave(); //System.out.println("Take("+num+"): left monitor"); return res; }
@Override public E poll(long timeout, TimeUnit unit) throws InterruptedException { final Monitor monitor = this.monitor; if (monitor.enterWhen(notEmpty, timeout, unit)) { try { return q.poll(); } finally { monitor.leave(); } } else { return null; } }
/** * See {@link java.util.concurrent.BlockingQueue#add()}, This method may block if the queue * is being updated. */ @SuppressWarnings("unchecked") @Override public synchronized boolean add(Runnable element) { boolean res; try { monitor.enterWhen(canAdd); } catch (InterruptedException e) { monitor.leave(); throw new RuntimeException(e); } res=this.delegate.add((PrioritizableRunnable<T>)element); monitor.leave(); return res; }
@Override public E take() throws InterruptedException { final Monitor monitor = this.monitor; monitor.enterWhen(notEmpty); try { return q.poll(); } finally { monitor.leave(); } }
/** * See {@link java.util.concurrent.BlockingQueue#offer()}, This method may block if the queue * is being updated. */ @SuppressWarnings("unchecked") @Override public synchronized boolean offer(Runnable o) { boolean res; try { monitor.enterWhen(canAdd); } catch (InterruptedException e) { monitor.leave(); throw new RuntimeException(e); } //System.out.println("offer: entered monitor"); res = this.delegate.offer((PrioritizableRunnable<T>) o); //System.out.println("offer: left monitor"); monitor.leave(); return res; }
/** * Inserts the specified element at the tail of this queue, waiting * for space to become available if the queue is full. * * @throws InterruptedException {@inheritDoc} * @throws NullPointerException {@inheritDoc} */ @Override public void put(E e) throws InterruptedException { if (e == null) throw new NullPointerException(); final Monitor monitor = this.monitor; monitor.enterWhen(notFull); try { insert(e); } finally { monitor.leave(); } }
@Override public E take() throws InterruptedException { final Monitor monitor = this.monitor; monitor.enterWhen(notEmpty); try { return extract(); } finally { monitor.leave(); } }
@Override public E poll(long timeout, TimeUnit unit) throws InterruptedException { final Monitor monitor = this.monitor; if (monitor.enterWhen(notEmpty, timeout, unit)) { try { return extract(); } finally { monitor.leave(); } } else { return null; } }
/** * Inserts the specified element at the tail of this queue, waiting * up to the specified wait time for space to become available if * the queue is full. * * @throws InterruptedException {@inheritDoc} * @throws NullPointerException {@inheritDoc} */ @Override public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException { if (e == null) throw new NullPointerException(); final Monitor monitor = this.monitor; if (monitor.enterWhen(notFull, timeout, unit)) { try { insert(e); return true; } finally { monitor.leave(); } } else { return false; } }