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

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

Introduction

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

Prototype

public void lock() 

Source Link

Document

Acquires the lock.

Usage

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

/**
 * Removes a single instance of the specified element from this queue, if it
 * is present, whether or not it has expired.
 *///from  w  w  w .  j  a va  2  s. c om
public boolean remove(Object o) {
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        for (TenantQueue q : qs.values()) {
            if (q.remove(o))
                return true;
        }
        return false;
    } 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
 * returned array elements are in no particular order.
 *
 * <p>//w w w. j a  v a2 s  .  c  om
 * The returned array will be "safe" in that no references to it are
 * maintained by this queue. (In other words, this method must allocate a
 * new array). The caller is thus free to modify the returned array.
 *
 * <p>
 * This method acts as bridge between array-based and collection-based APIs.
 *
 * @return an array containing all of the elements in this queue
 */
public Object[] toArray() {
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        Object[] arr = new Object[size];
        int i = 0;
        for (TenantQueue q : qs.values()) {
            Object[] qarr = q.toArray();
            System.arraycopy(qarr, 0, arr, i, qarr.length);
            i += qarr.length;
        }
        return arr;
    } finally {
        lock.unlock();
    }
}

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

/**
 * Retrieves and removes the head of this queue, or returns <tt>null</tt> if
 * this queue has no elements with an expired delay.
 *
 * @return the head of this queue, or <tt>null</tt> if this queue has no
 *         elements with an expired delay
 *//*from   w  ww.j  a va2 s . c om*/
public E poll() {
    final ReentrantLock lock = this.lock;
    lock.lock();
    long t = now();
    TenantQueue.Item item = null;
    try {
        TenantQueue q = nextQueue(t);
        if (q == null || q.next > t)
            return null;
        else {
            item = q.poll(t);
            return item == null ? null : item.element;
        }
    } finally {
        lock.unlock();
        done(item, t);
    }
}

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

/**
 * Inserts the specified element into this delay queue.
 *
 * @param e//  ww  w.  jav  a  2s .  c o m
 *            the element to add
 * @return <tt>true</tt>
 * @throws NullPointerException
 *             if the specified element is null
 */
public boolean offer(E e) {
    final ReentrantLock lock = this.lock;
    int tenant = tenant();
    lock.lock();
    try {
        long t = now();
        queue(tenant).offer(e, t);
        if (peekNext(t) == e) {
            leader = null;
            available.signal();
        }
        return true;
    } finally {
        lock.unlock();
        taggregator.throttling(tenant);
    }
}

From source file:me.xiaopan.android.gohttp.HttpRequestHandler.java

@Override
public void run() {
    if (httpRequest.isCanceled()) {
        httpRequest.finish();/*from   ww w .java 2s .  c om*/
        new CancelRunnable(httpRequest).execute();
        if (httpRequest.getGoHttp().isDebugMode())
            Log.w(GoHttp.LOG_TAG,
                    httpRequest.getName() + "; " + "Canceled : " + "; " + httpRequest.getUrl());
        return;
    }
    if (httpRequest.getGoHttp().isDebugMode())
        Log.d(GoHttp.LOG_TAG,
                httpRequest.getName() + "; " + "Started : " + "; " + httpRequest.getUrl());

    boolean isCache = httpRequest.getCacheConfig() != null;
    ReentrantLock reentrantLock = null;

    if (isCache) {
        reentrantLock = httpRequest.getGoHttp().getSyncManager()
                .getLockByCacheId(httpRequest.getCacheConfig().getId());
        reentrantLock.lock();

        if (httpRequest.isCanceled()) {
            httpRequest.finish();
            new CancelRunnable(httpRequest).execute();
            if (httpRequest.getGoHttp().isDebugMode())
                Log.w(GoHttp.LOG_TAG, httpRequest.getName() + "; " + "Canceled : ??" + "; "
                        + httpRequest.getUrl());
            reentrantLock.unlock();
            return;
        }
    }

    boolean isRefreshCache = isCache && httpRequest.getCacheConfig().isRefreshCache();
    boolean isContinueCallback = true;
    HttpResponse httpResponse = null;

    // ??
    if (isCache && httpRequest.getGoHttp().getCacheManager().isHasAvailableCache(httpRequest)) {
        if (httpRequest.isCanceled()) {
            httpRequest.finish();
            new CancelRunnable(httpRequest).execute();
            if (httpRequest.getGoHttp().isDebugMode())
                Log.w(GoHttp.LOG_TAG, httpRequest.getName() + "; " + "Canceled : ?" + "; "
                        + httpRequest.getUrl());
            reentrantLock.unlock();
            return;
        }
        if (httpRequest.getGoHttp().isDebugMode())
            Log.d(GoHttp.LOG_TAG,
                    httpRequest.getName() + "; " + "Cache : ?" + "; " + httpRequest.getUrl());

        // ?Http?
        httpResponse = httpRequest.getGoHttp().getCacheManager().readHttpResponseFromCache(httpRequest);
        if (httpRequest.isCanceled()) {
            httpRequest.finish();
            new CancelRunnable(httpRequest).execute();
            if (httpRequest.getGoHttp().isDebugMode())
                Log.w(GoHttp.LOG_TAG, httpRequest.getName() + "; " + "Canceled : ?" + "; "
                        + httpRequest.getUrl());
            reentrantLock.unlock();
            return;
        }

        if (httpResponse != null) {
            try {
                Object responseObject = httpRequest.getResponseHandler().handleResponse(httpRequest,
                        httpResponse);
                if (responseObject == null) {
                    throw new Exception("response object is null");
                }

                // ???
                isContinueCallback = isRefreshCache && httpRequest.getCacheConfig().isRefreshCallback();
                if (!(responseObject instanceof HttpRequest.Failure)
                        && httpRequest.getResponseHandleCompletedAfterListener() != null) {
                    //noinspection unchecked
                    Object response = httpRequest.getResponseHandleCompletedAfterListener()
                            .onResponseHandleAfter(httpRequest, httpResponse, responseObject, true,
                                    isContinueCallback);
                    if (response != null) {
                        responseObject = response;
                    }
                }
                if (httpRequest.isCanceled()) {
                    httpRequest.finish();
                    new CancelRunnable(httpRequest).execute();
                    if (httpRequest.getGoHttp().isDebugMode())
                        Log.w(GoHttp.LOG_TAG, httpRequest.getName() + "; " + "Canceled : ??"
                                + "; " + httpRequest.getUrl());
                    reentrantLock.unlock();
                    return;
                }

                // ?????
                if (!isRefreshCache) {
                    httpRequest.finish();
                }
                if (responseObject instanceof HttpRequest.Failure) {
                    new FailedRunnable(httpRequest, httpResponse, (HttpRequest.Failure) responseObject, true,
                            isContinueCallback).execute();
                } else {
                    new CompletedRunnable(httpRequest, httpResponse, responseObject, true, isContinueCallback)
                            .execute();
                }
                // ?????
                if (!isRefreshCache) {
                    if (httpRequest.getGoHttp().isDebugMode())
                        Log.d(GoHttp.LOG_TAG,
                                httpRequest.getName() + "; "
                                        + "Completed : ???" + "; "
                                        + httpRequest.getUrl());
                    reentrantLock.unlock();
                    return;
                }
                if (httpRequest.getGoHttp().isDebugMode())
                    Log.d(GoHttp.LOG_TAG, httpRequest.getName() + "; " + "Cache : ??"
                            + "; " + httpRequest.getUrl());
            } catch (Throwable e) {
                e.printStackTrace();
                if (httpRequest.isCanceled()) {
                    httpRequest.finish();
                    new CancelRunnable(httpRequest).execute();
                    if (httpRequest.getGoHttp().isDebugMode())
                        Log.w(GoHttp.LOG_TAG,
                                httpRequest.getName() + "; "
                                        + "Canceled : ?Http??" + "; "
                                        + httpRequest.getUrl());
                    reentrantLock.unlock();
                    return;
                }
                if (httpRequest.getGoHttp().isDebugMode())
                    Log.e(GoHttp.LOG_TAG,
                            httpRequest.getName() + "; " + "Failed : ?Http??"
                                    + "; " + httpRequest.getUrl());

                new FailedRunnable(httpRequest, httpResponse, new HttpRequest.Failure(e), true,
                        isContinueCallback).execute();
            }
        }
    }

    // ??
    try {
        httpResponse = httpRequest.getGoHttp().getNetManager().getHttpResponse(httpRequest);
    } catch (Throwable e) {
        e.printStackTrace();
        releaseConnect(httpResponse);
        httpRequest.finish();
        if (httpRequest.isCanceled()) {
            if (isContinueCallback) {
                new CancelRunnable(httpRequest).execute();
            }
            if (httpRequest.getGoHttp().isDebugMode())
                Log.w(GoHttp.LOG_TAG, httpRequest.getName() + "; "
                        + "Canceled : ?Http??" + "; " + httpRequest.getUrl());
            if (reentrantLock != null)
                reentrantLock.unlock();
            return;
        }
        if (httpRequest.getGoHttp().isDebugMode())
            Log.e(GoHttp.LOG_TAG, httpRequest.getName() + "; "
                    + "Failed : ?Http??" + "; " + httpRequest.getUrl());

        if (isContinueCallback) {
            new FailedRunnable(httpRequest, httpResponse, new HttpRequest.Failure(e), false, false).execute();
        }
        if (reentrantLock != null)
            reentrantLock.unlock();
        return;
    }
    if (httpRequest.isCanceled()) {
        releaseConnect(httpResponse);
        httpRequest.finish();
        new CancelRunnable(httpRequest).execute();
        if (httpRequest.getGoHttp().isDebugMode())
            Log.w(GoHttp.LOG_TAG, httpRequest.getName() + "; " + "Canceled : ?Http?"
                    + "; " + httpRequest.getUrl());
        if (reentrantLock != null)
            reentrantLock.unlock();
        return;
    }
    if (httpRequest.getGoHttp().isDebugMode())
        Log.d(GoHttp.LOG_TAG, httpRequest.getName() + "; " + "Net : ?Http??" + "; "
                + httpRequest.getUrl());

    // Http?
    if (isCache && httpRequest.getResponseHandler().canCache(httpResponse)) {
        try {
            httpRequest.getGoHttp().getCacheManager().saveHttpResponseToCache(httpRequest, httpResponse);
        } catch (IOException e) {
            e.printStackTrace();
            releaseConnect(httpResponse);
            httpRequest.finish();
            if (httpRequest.isCanceled()) {
                if (isContinueCallback) {
                    new CancelRunnable(httpRequest).execute();
                }
                if (httpRequest.getGoHttp().isDebugMode())
                    Log.w(GoHttp.LOG_TAG, httpRequest.getName() + "; "
                            + "Canceled : Http??" + "; " + httpRequest.getUrl());
                reentrantLock.unlock();
                return;
            }
            if (httpRequest.getGoHttp().isDebugMode())
                Log.d(GoHttp.LOG_TAG, httpRequest.getName() + "; " + "Failed : Http??"
                        + "; " + httpRequest.getUrl());
            if (isContinueCallback) {
                new FailedRunnable(httpRequest, httpResponse, new HttpRequest.Failure(e), false, false)
                        .execute();
            }
            reentrantLock.unlock();
            return;
        }
        if (httpRequest.isCanceled()) {
            httpRequest.finish();
            new CancelRunnable(httpRequest).execute();
            if (httpRequest.getGoHttp().isDebugMode())
                Log.w(GoHttp.LOG_TAG, httpRequest.getName() + "; " + "Canceled : Http?" + "; "
                        + httpRequest.getUrl());
            reentrantLock.unlock();
            return;
        }
        if (httpRequest.getGoHttp().isDebugMode())
            Log.d(GoHttp.LOG_TAG, httpRequest.getName() + "; " + "Cache : Http??" + "; "
                    + httpRequest.getUrl());
    }

    // ???
    if (!isContinueCallback) {
        httpRequest.finish();
        if (httpRequest.getGoHttp().isDebugMode())
            Log.d(GoHttp.LOG_TAG, httpRequest.getName() + "; " + "Completed : ??" + "; "
                    + httpRequest.getUrl());
        reentrantLock.unlock();
        return;
    }

    // ??
    Object responseObject;
    try {
        responseObject = httpRequest.getResponseHandler().handleResponse(httpRequest, httpResponse);
        if (responseObject == null) {
            throw new Exception("response object is null");
        }

        // ???
        if (!(responseObject instanceof HttpRequest.Failure)
                && httpRequest.getResponseHandleCompletedAfterListener() != null) {
            //noinspection unchecked
            Object response = httpRequest.getResponseHandleCompletedAfterListener()
                    .onResponseHandleAfter(httpRequest, httpResponse, responseObject, false, false);
            if (response != null) {
                responseObject = response;
            }
        }
    } catch (Throwable e) {
        e.printStackTrace();
        releaseConnect(httpResponse);
        httpRequest.finish();

        if (httpRequest.isCanceled()) {
            new CancelRunnable(httpRequest).execute();
            if (httpRequest.getGoHttp().isDebugMode())
                Log.w(GoHttp.LOG_TAG,
                        httpRequest.getName() + "; "
                                + "Canceled : ??Http??" + "; "
                                + httpRequest.getUrl());
            if (reentrantLock != null)
                reentrantLock.unlock();
            return;
        }
        if (httpRequest.getGoHttp().isDebugMode())
            Log.e(GoHttp.LOG_TAG,
                    httpRequest.getName() + "; " + "Failed : ??Http??"
                            + "; " + httpRequest.getUrl());

        new FailedRunnable(httpRequest, httpResponse, new HttpRequest.Failure(e), false, false).execute();
        if (reentrantLock != null)
            reentrantLock.unlock();
        return;
    }
    if (httpRequest.isCanceled()) {
        releaseConnect(httpResponse);
        httpRequest.finish();
        new CancelRunnable(httpRequest).execute();
        if (httpRequest.getGoHttp().isDebugMode())
            Log.w(GoHttp.LOG_TAG, httpRequest.getName() + "; " + "Canceled : ?Http?" + "; "
                    + httpRequest.getUrl());
        if (reentrantLock != null)
            reentrantLock.unlock();
        return;
    }
    if (httpRequest.getGoHttp().isDebugMode())
        Log.d(GoHttp.LOG_TAG,
                httpRequest.getName() + "; " + "Net : ?Http?" + "; " + httpRequest.getUrl());

    // 
    if (responseObject instanceof HttpRequest.Failure) {
        HttpRequest.Failure failure = (HttpRequest.Failure) responseObject;
        new FailedRunnable(httpRequest, httpResponse, failure, false, false).execute();
        if (httpRequest.getGoHttp().isDebugMode())
            Log.e(GoHttp.LOG_TAG, httpRequest.getName() + "; " + "Failed : " + failure.toString() + "; "
                    + httpRequest.getUrl());
    } else {
        new CompletedRunnable(httpRequest, httpResponse, responseObject, false, false).execute();
        if (httpRequest.getGoHttp().isDebugMode())
            Log.d(GoHttp.LOG_TAG,
                    httpRequest.getName() + "; " + "Completed : ?" + "; " + httpRequest.getUrl());
    }
    httpRequest.finish();
    if (reentrantLock != null)
        reentrantLock.unlock();
}

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

@Override
public void cancelWorkflow(final WorkflowInstance workflowInstance) {
    if (!isEnabled()) {
        return;//from  w w w  . java2 s  .  com
    }
    if (DEBUG_ENABLED) {
        LOGGER.debug("CANCEL WORKFLOW (task) " + workflowInstance.getId());
    }
    ReentrantLock acquiredLock = acquireLock(CMFService.getTenantId());
    acquiredLock.lock();
    try {
        AuthenticationUtil.runAs(new RunAsWork<Void>() {

            @Override
            public Void doWork() throws Exception {
                fixWorkflowMetadata(null, workflowInstance, CANCELLED,
                        getWorkflowItems(workflowInstance.getWorkflowPackage(), CMFModel.PROP_TYPE));
                return null;
            }
        }, CMFService.getSystemUser());
    } catch (Exception e) {
        LOGGER.error(e);
        // just print and skip
    } finally {
        acquiredLock.unlock();
    }
}

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

@Override
public void addTask(final WorkflowPath workflowPath) {
    if (!isEnabled()) {
        return;/* w ww .j  a  v a2  s .c om*/
    }
    if (DEBUG_ENABLED) {
        LOGGER.debug("ADD TASK FOR PATH " + workflowPath.getId());
    }
    ReentrantLock acquiredLock = acquireLock(CMFService.getTenantId());
    acquiredLock.lock();
    try {
        WorkflowInstance instance = workflowPath.getInstance();
        final List<WorkflowTask> queryTasks = listTasks(null, instance.getId());
        AuthenticationUtil.runAs(new RunAsWork<NodeRef>() {

            @Override
            public NodeRef doWork() throws Exception {
                WorkflowTask task = queryTasks.get(0);
                return addTaskInternal(task);

            }
        }, CMFService.getSystemUser());
    } catch (Exception e) {
        LOGGER.error(e);
    } finally {
        acquiredLock.unlock();
    }
}

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

@Override
public NodeRef addTask(final WorkflowTask task) {
    if (!isEnabled()) {
        return null;
    }/*from  w w w . j  a va2s.c  o m*/
    if (DEBUG_ENABLED) {
        LOGGER.debug("ADD TASK " + task.getId());
    }
    ReentrantLock acquiredLock = acquireLock(CMFService.getTenantId());
    acquiredLock.lock();
    try {
        final List<WorkflowTask> queryTasks = listTasks(task.getId(), null);
        return AuthenticationUtil.runAs(new RunAsWork<NodeRef>() {

            @Override
            public NodeRef doWork() throws Exception {
                WorkflowTask task = queryTasks.get(0);
                return addTaskInternal(task);

            }
        }, CMFService.getSystemUser());
    } catch (Exception e) {
        LOGGER.error(e);
    } finally {
        acquiredLock.unlock();
    }
    return null;
}

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

@Override
public NodeRef addStandaloneTask(final String taskId, final Map<QName, Serializable> props) {
    if (!isEnabled()) {
        return null;
    }//from w  ww .  j  a va2s. c om
    if (DEBUG_ENABLED) {
        LOGGER.debug("ADD TASK FOR STANDALONE " + taskId);
    }
    ReentrantLock acquiredLock = acquireLock(CMFService.getTenantId());
    acquiredLock.lock();
    try {
        final List<WorkflowTask> queryTasks = listTasks(taskId, null);
        return AuthenticationUtil.runAs(new RunAsWork<NodeRef>() {

            @Override
            public NodeRef doWork() throws Exception {
                WorkflowTask task = queryTasks.get(0);
                task.getProperties().putAll(props);
                return addTaskInternal(task);

            }
        }, CMFService.getSystemUser());
    } catch (Exception e) {
        LOGGER.error(e);
    } finally {
        acquiredLock.unlock();
    }
    return null;
}

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

@Override
public NodeRef getTaskNode(final WorkflowTask task) {
    ReentrantLock acquiredLock = acquireLock(CMFService.getTenantId());
    acquiredLock.lock();
    try {/*ww  w. j  av  a2  s  .co  m*/
        if (taskCache.contains(task.getId())) {
            return taskCache.get(task.getId());
        }
        return AuthenticationUtil.runAs(new RunAsWork<NodeRef>() {
            @Override
            public NodeRef doWork() throws Exception {
                ResultSet query = registry.getSearchService().query(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE,
                        SearchService.LANGUAGE_SOLR_FTS_ALFRESCO, buildTaskQuery(task));
                List<NodeRef> nodeRefs = query.getNodeRefs();
                if ((nodeRefs == null) || nodeRefs.size() != 1) {
                    return null;
                }
                query = null;
                return nodeRefs.get(0);
            }

        }, CMFService.getSystemUser());
    } catch (Exception e) {
        LOGGER.error(e);
    } finally {
        acquiredLock.unlock();
    }
    return null;

}