Example usage for java.util.concurrent.locks ReentrantLock unlock

List of usage examples for java.util.concurrent.locks ReentrantLock unlock

Introduction

In this page you can find the example usage for java.util.concurrent.locks ReentrantLock unlock.

Prototype

public void unlock() 

Source Link

Document

Attempts to release this lock.

Usage

From source file:org.apache.openejb.resource.jdbc.dbcp.BasicDataSource.java

public Logger getParentLogger() throws SQLFeatureNotSupportedException {
    final ReentrantLock l = lock;
    l.lock();//from  w ww  .jav a 2s  . co m
    try {

        if (null == this.logger) {
            this.logger = (Logger) Reflections.invokeByReflection(super.dataSource, "getParentLogger",
                    new Class<?>[0], null);
        }

        return this.logger;
    } catch (final Throwable e) {
        throw new SQLFeatureNotSupportedException();
    } finally {
        l.unlock();
    }
}

From source file:com.chicm.cmraft.util.CappedPriorityBlockingQueue.java

@Override
public boolean offer(E e) {
    boolean ret = false;
    final ReentrantLock putLock = this.putLock;
    try {// ww w.  ja  v  a2 s .co m
        putLock.lockInterruptibly();
        while (size() >= this.maxCapacity) {
            LOG.debug("Thread:" + Thread.currentThread().getName() + ": queue is full, waiting...");
            notFull.await();
            LOG.debug("Thread:" + Thread.currentThread().getName() + ": waiting done");
        }

        ret = super.offer(e);
        if (size() < this.maxCapacity)
            notFull.signal();
    } catch (InterruptedException ex) {
        LOG.error("InterruptedException", ex);
        return false;
    } finally {
        putLock.unlock();
    }
    return ret;
}

From source file:org.commoncrawl.service.parser.client.Dispatcher.java

public ParserNode take() {
    final ReentrantLock lock = this.lock;
    lock.lock();//  w w  w.j  a v a  2 s  .  com
    try {
        try {
            while (_onlineNodes.size() == 0)
                notEmpty.await();
        } catch (InterruptedException ie) {
            if (online.get()) {
                notEmpty.signal(); // propagate to non-interrupted thread
            }
        }
        ParserNode x = _onlineNodes.poll();
        x.touch();
        assert x != null;
        _onlineNodes.add(x);
        return x;
    } finally {
        lock.unlock();
    }
}

From source file:no.sesat.search.http.filters.SiteLocatorFilter.java

private static void doChainFilter(final FilterChain chain, final HttpServletRequest request,
        final HttpServletResponse response) throws IOException, ServletException {

    final HttpSession session = request.getSession();

    // fetch the user's deque
    final Deque<ServletRequest> deque = getUsersDeque(session);

    // lock to execute
    final ReentrantLock lock = (ReentrantLock) session.getAttribute(USER_REQUEST_LOCK);

    // deque has a time limit. start counting.
    long timeLeft = WAIT_TIME;

    try {/*from ww  w .  ja  va 2  s  .  c  o  m*/
        // attempt to join deque
        if (deque.offerFirst(request)) {
            timeLeft = tryLock(request, deque, lock, timeLeft);
        }

        if (lock.isHeldByCurrentThread()) {

            // waiting is over. and we can execute
            chain.doFilter(request, response);

        } else {
            // we failed to execute. return 409 response.
            if (response instanceof HttpServletResponse) {

                LOG.warn(" -- response 409 "
                        + (0 < timeLeft ? "(More then " + REQUEST_QUEUE_SIZE + " requests already in queue)"
                                : "(Timeout: Waited " + WAIT_TIME + " ms)"));

                response.sendError(HttpServletResponse.SC_CONFLICT);
            }
        }
    } finally {

        // take out of deque first
        deque.remove(request);

        // release the lock, waiting up the next request
        if (lock.isHeldByCurrentThread()) {
            lock.unlock();
        }
    }
}

From source file:edu.brown.cs.systems.retro.throttling.throttlingqueues.ThrottlingLockingQueue.java

public int size() {
    final ReentrantLock lock = this.lock;
    lock.lock();/* ww  w. j av a  2s .com*/
    try {
        return size;
    } finally {
        lock.unlock();
    }
}

From source file:edu.brown.cs.systems.retro.throttling.throttlingqueues.ThrottlingLockingQueue.java

/**
 * Retrieves, but does not remove, the head of this queue, or returns
 * <tt>null</tt> if this queue is empty. Unlike <tt>poll</tt>, if no expired
 * elements are available in the queue, this method returns the element that
 * will expire next, if one exists.//ww w .j a va  2  s  .  c  o  m
 *
 * @return the head of this queue, or <tt>null</tt> if this queue is empty.
 */
public E peek() {
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        return peekNext(now());
    } finally {
        lock.unlock();
    }
}

From source file:edu.brown.cs.systems.retro.throttling.throttlingqueues.ThrottlingLockingQueue.java

/**
 * Returns an array containing all of the elements in this queue; the
 * runtime type of the returned array is that of the specified array. The
 * returned array elements are in no particular order. If the queue fits in
 * the specified array, it is returned therein. Otherwise, a new array is
 * allocated with the runtime type of the specified array and the size of
 * this queue.//  w w  w.j a  va2s . c  o  m
 *
 * <p>
 * If this queue fits in the specified array with room to spare (i.e., the
 * array has more elements than this queue), the element in the array
 * immediately following the end of the queue is set to <tt>null</tt>.
 *
 * <p>
 * Like the {@link #toArray()} method, this method acts as bridge between
 * array-based and collection-based APIs. Further, this method allows
 * precise control over the runtime type of the output array, and may, under
 * certain circumstances, be used to save allocation costs.
 *
 * <p>
 * The following code can be used to dump a delay queue into a newly
 * allocated array of <tt>Delayed</tt>:
 *
 * <pre>
 * Delayed[] a = q.toArray(new Delayed[0]);
 * </pre>
 *
 * Note that <tt>toArray(new Object[0])</tt> is identical in function to
 * <tt>toArray()</tt>.
 *
 * @param a
 *            the array into which the elements of the queue are to be
 *            stored, if it is big enough; otherwise, a new array of the
 *            same runtime type is allocated for this purpose
 * @return an array containing all of the elements in this queue
 * @throws ArrayStoreException
 *             if the runtime type of the specified array is not a supertype
 *             of the runtime type of every element in this queue
 * @throws NullPointerException
 *             if the specified array is null
 */
public <T> T[] toArray(T[] a) {
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        return (T[]) toArray(a);
    } finally {
        lock.unlock();
    }
}

From source file:edu.brown.cs.systems.retro.throttling.throttlingqueues.ThrottlingLockingQueue.java

public void clearRate(int tenantId) {
    final ReentrantLock lock = this.lock;
    lock.lock();/*from   w w w . ja va 2s  .  c  o  m*/
    try {
        queue(tenantId).clearRate();
    } finally {
        lock.unlock();
    }
}

From source file:edu.brown.cs.systems.retro.throttling.throttlingqueues.ThrottlingLockingQueue.java

public void setRate(int tenantId, double rate) {
    final ReentrantLock lock = this.lock;
    lock.lock();//  w w w  . j  av a 2 s.c o m
    try {
        queue(tenantId).setRate(rate);
    } finally {
        lock.unlock();
    }
}

From source file:edu.brown.cs.systems.retro.throttling.throttlingqueues.ThrottlingLockingQueue.java

/**
 * Atomically removes all of the elements from this delay queue. The queue
 * will be empty after this call returns. Elements with an unexpired delay
 * are not waited for; they are simply discarded from the queue.
 *//*from  w  w  w  . j  a  v a  2s . co  m*/
public void clear() {
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        for (TenantQueue q : qs.values()) {
            q.clear();
        }
    } finally {
        lock.unlock();
    }
}