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.cassandra.concurrent.ContinuationsExecutor.java

/**
 * If there is a security manager, makes sure caller has permission to shut
 * down threads in general (see shutdownPerm). If this passes, additionally
 * makes sure the caller is allowed to interrupt each worker thread. This
 * might not be true even if first check passed, if the SecurityManager
 * treats some threads specially.//from  w ww .  jav  a2  s .  c o  m
 */
private void checkShutdownAccess() {
    SecurityManager security = System.getSecurityManager();
    if (security != null) {
        security.checkPermission(shutdownPerm);
        final ReentrantLock mainLock = this.mainLock;
        mainLock.lock();
        try {
            for (Worker w : workers)
                security.checkAccess(w.thread);
        } finally {
            mainLock.unlock();
        }
    }
}

From source file:org.apache.cassandra.concurrent.ContinuationsExecutor.java

/**
 * Attempts to stop all actively executing tasks, halts the processing of
 * waiting tasks, and returns a list of the tasks that were awaiting
 * execution. These tasks are drained (removed) from the task queue upon
 * return from this method.//from  ww w . j av a  2s .  c  o  m
 * 
 * <p>
 * There are no guarantees beyond best-effort attempts to stop processing
 * actively executing tasks. This implementation cancels tasks via
 * {@link Thread#interrupt}, so any task that fails to respond to
 * interrupts may never terminate.
 * 
 * @throws SecurityException
 *             {@inheritDoc}
 */
public List<Runnable> shutdownNow() {
    List<Runnable> tasks;
    final ReentrantLock mainLock = this.mainLock;
    mainLock.lock();
    try {
        checkShutdownAccess();
        advanceRunState(STOP);
        interruptWorkers();
        tasks = drainQueue();
    } finally {
        mainLock.unlock();
    }
    tryTerminate();
    return tasks;
}

From source file:org.apache.cassandra.concurrent.ContinuationsExecutor.java

/**
 * Returns the approximate total number of tasks that have completed
 * execution. Because the states of tasks and threads may change dynamically
 * during computation, the returned value is only an approximation, but one
 * that does not ever decrease across successive calls.
 * //from ww  w.ja v a2 s.c o  m
 * @return the number of tasks
 */
public long getCompletedTaskCount() {
    final ReentrantLock mainLock = this.mainLock;
    mainLock.lock();
    try {
        long n = completedTaskCount;
        for (Worker w : workers)
            n += w.completedTasks;
        return n;
    } finally {
        mainLock.unlock();
    }
}

From source file:org.apache.cassandra.concurrent.ContinuationsExecutor.java

/**
 * Returns the approximate number of threads that are actively executing
 * tasks./* w ww.  j  a  va2s .c o m*/
 * 
 * @return the number of threads
 */
public int getActiveCount() {
    final ReentrantLock mainLock = this.mainLock;
    mainLock.lock();
    try {
        int n = 0;
        for (Worker w : workers)
            if (w.isLocked())
                ++n;
        return n;
    } finally {
        mainLock.unlock();
    }
}

From source file:org.apache.cassandra.concurrent.ContinuationsExecutor.java

/**
 * Returns the current number of threads in the pool.
 * /*from   www . ja v  a  2  s. co  m*/
 * @return the number of threads
 */
public int getPoolSize() {
    final ReentrantLock mainLock = this.mainLock;
    mainLock.lock();
    try {
        // Remove rare and surprising possibility of
        // isTerminated() && getPoolSize() > 0
        return runStateAtLeast(ctl.get(), TIDYING) ? 0 : workers.size();
    } finally {
        mainLock.unlock();
    }
}

From source file:org.apache.cassandra.concurrent.ContinuationsExecutor.java

/**
 * Interrupts all threads, even if active. Ignores SecurityExceptions (in
 * which case some threads may remain uninterrupted).
 *//*from  w  w  w . ja  v  a  2  s .c o  m*/
private void interruptWorkers() {
    final ReentrantLock mainLock = this.mainLock;
    mainLock.lock();
    try {
        for (Worker w : workers) {
            try {
                w.thread.interrupt();
            } catch (SecurityException ignore) {
            }
        }
    } finally {
        mainLock.unlock();
    }
}

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

/**
 * Retrieves and removes the head of this queue, waiting if necessary until
 * an element with an expired delay is available on this queue, or the
 * specified wait time expires./*from ww w. java  2 s.  c o m*/
 *
 * @return the head of this queue, or <tt>null</tt> if the specified waiting
 *         time elapses before an element with an expired delay becomes
 *         available
 * @throws InterruptedException
 *             {@inheritDoc}
 */
public E poll(long timeout, TimeUnit unit) throws InterruptedException {
    long nanos = unit.toNanos(timeout);
    final ReentrantLock lock = this.lock;
    lock.lockInterruptibly();
    long t = now();
    TenantQueue.Item item = null;
    try {
        for (;;) {
            TenantQueue q = nextQueue(t);
            if (q == null) {
                if (nanos <= 0)
                    return null;
                else
                    nanos = available.awaitNanos(nanos);
            } else {
                long delay = q.next - t;
                if (delay <= 0) {
                    item = q.poll(t);
                    return item == null ? null : item.element;
                }
                if (nanos <= 0)
                    return null;
                if (nanos < delay || leader != null)
                    nanos = available.awaitNanos(nanos);
                else {
                    Thread thisThread = Thread.currentThread();
                    leader = thisThread;
                    try {
                        long timeLeft = available.awaitNanos(delay);
                        nanos -= delay - timeLeft;
                    } finally {
                        if (leader == thisThread)
                            leader = null;
                    }
                }
            }
            t = System.nanoTime();
        }
    } finally {
        if (leader == null && hasNext())
            available.signal();
        lock.unlock();
        done(item, t);
    }
}

From source file:org.alfresco.repo.workflow.WorkflowReportServiceImpl.java

/**
 * Invoked on delete event. Not implemented yet
 *
 * @param workflowId//  w ww  . j  a  va2 s  .  c  om
 *            is the workflow id
 */
@Override
public void deleteWorkflow(final String workflowId) {
    if (!isEnabled()) {
        return;
    }
    ReentrantLock acquiredLock = acquireLock(CMFService.getTenantId());
    acquiredLock.lock();
    try {
        AuthenticationUtil.runAs(new RunAsWork<Void>() {

            @Override
            public Void doWork() throws Exception {
                try {
                    List<WorkflowPath> workflowById = workflowService.getWorkflowPaths(workflowId);
                    for (WorkflowPath workflowPath : workflowById) {
                        List<WorkflowTask> tasksForWorkflowPath = workflowService
                                .getTasksForWorkflowPath(workflowPath.getId());
                        for (WorkflowTask workflowTask : tasksForWorkflowPath) {
                            deleteTask(workflowTask);
                        }
                    }
                } catch (Exception e) {
                    // just print and skip so not to break alfresco code
                    LOGGER.error(e);
                }
                return null;
            }
        }, CMFService.getSystemUser());
    } catch (Exception e) {
        LOGGER.error(e);
    } finally {
        acquiredLock.unlock();
    }
}

From source file:org.apache.cassandra.concurrent.ContinuationsExecutor.java

/**
 * Returns the approximate total number of tasks that have ever been
 * scheduled for execution. Because the states of tasks and threads may
 * change dynamically during computation, the returned value is only an
 * approximation./*from w  w  w  .ja v a  2  s .c o m*/
 * 
 * @return the number of tasks
 */
public long getTaskCount() {
    final ReentrantLock mainLock = this.mainLock;
    mainLock.lock();
    try {
        long n = completedTaskCount;
        for (Worker w : workers) {
            n += w.completedTasks;
            if (w.isLocked())
                ++n;
        }
        return n + workQueue.size();
    } finally {
        mainLock.unlock();
    }
}

From source file:org.apache.cassandra.concurrent.ContinuationsExecutor.java

public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
    long nanos = unit.toNanos(timeout);
    final ReentrantLock mainLock = this.mainLock;
    mainLock.lock();/*w w  w .  j a v a 2s.  c o  m*/
    try {
        for (;;) {
            if (runStateAtLeast(ctl.get(), TERMINATED))
                return true;
            if (nanos <= 0)
                return false;
            nanos = termination.awaitNanos(nanos);
        }
    } finally {
        mainLock.unlock();
    }
}