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:Main.java

/**
 * signal all on a condition//w w w .  jav a  2  s  . c o  m
 * @param lock
 * @param cond
 */
public static void signalAll(ReentrantLock lock, Condition cond) {
    lock.lock();
    try {
        cond.signalAll();
    } finally {
        lock.unlock();
    }
}

From source file:Main.java

/**
 * signal a condition//w  w w  .j av  a 2  s.  c o m
 * @param lock
 * @param cond
 */
public static void signal(ReentrantLock lock, Condition cond) {
    lock.lock();
    try {
        cond.signal();
    } finally {
        lock.unlock();
    }
}

From source file:smanilov.mandelbrot.compute.Computer.java

private static void saveImage(Image drawing, ReentrantLock drawingLock) {
    drawingLock.lock();
    try {//  ww  w . j  a va 2s .  com
        ImageIO.write((RenderedImage) drawing, "png", new File("mandelbrot.png"));
    } catch (IOException e) {
        e.printStackTrace();
    }
    drawingLock.unlock();
}

From source file:eu.riscoss.server.DBConnector.java

private static void lock(String domain) {

    ReentrantLock lock = locks.get(domain);

    if (lock == null) {
        lock = new ReentrantLock(true);
        locks.put(domain, lock);/*from  w w  w. jav a  2  s .c  o m*/
    }

    System.out.println("'" + domain + "' locked by " + Thread.currentThread());
    //      Thread.dumpStack();
    lock.lock();
}

From source file:com.shigengyu.hyperion.cache.LocalWorkflowInstanceCache.java

@Override
public <T extends WorkflowInstance> WorkflowInstance acquire(final Integer workflowInstanceId) {
    try {//w w w . j av  a 2  s . c om
        ReentrantLock lock = getLock(workflowInstanceId);
        lock.lock();

        WorkflowInstance workflowInstance = cache.get(workflowInstanceId);
        return workflowInstance;
    } catch (final ExecutionException e) {
        throw new WorkflowStateException("Failed to get workflow instance [{}]", workflowInstanceId, e);
    }
}

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

/**
 * Signals a waiting put. Called only from take/poll.
 *//*w  ww .  j  a  v  a  2  s  .co m*/
private void signalNotFull() {
    final ReentrantLock putLock = this.putLock;
    putLock.lock();
    try {
        LOG.debug("singal not full");
        notFull.signal();
    } finally {
        putLock.unlock();
    }
}

From source file:org.broadleafcommerce.core.web.order.SessionOrderLockManager.java

/**
 * Note that although this method accepts an {@link Order} parameter, it does not use it in any way. This 
 * session-based lock manager implementation will prevent all operations that are identified as requiring a
 * lock from happening in parallel. Instead, it will execute them sequentially as locks are released from 
 * previous implementations./*from  www .  j  av  a  2  s. co m*/
 */
@Override
public Object acquireLock(Order order) {
    ReentrantLock lockObject = getSessionLock();
    lockObject.lock();
    return lockObject;
}

From source file:net.ymate.platform.webmvc.support.WebCacheProcessor.java

private PageMeta __doPutCacheElement(GenericResponseWrapper response, ICaches caches,
        ResponseCache responseCache, String cacheKey, IView resultView) throws Exception {
    ReentrantLock _locker = __doGetCacheLocker(cacheKey);
    _locker.lock();
    ////  ww  w  . ja v a  2  s.c  o m
    PageMeta _element = null;
    try {
        // ??
        _element = (PageMeta) caches.get(responseCache.cacheName(), cacheKey);
        // ?
        if (_element == null || _element.isExpired()) {
            // ??
            ByteArrayOutputStream _output = new ByteArrayOutputStream();
            resultView.render(_output);

            _element = new PageMeta(response.getContentType(), response.getHeaders(), _output.toByteArray(),
                    responseCache.useGZip());
            // 
            int _timeout = responseCache.timeout() > 0 ? responseCache.timeout()
                    : caches.getModuleCfg().getDefaultCacheTimeout();
            if (_timeout > 0) {
                _element.setTimeout(_timeout);
            }
            // 
            caches.put(responseCache.cacheName(), cacheKey, _element);
        }
    } catch (UnsupportedOperationException e) {
        _LOG.warn(
                resultView.getClass().getName() + " Unsupported Render To OutputStream Operation, Skip Cache.");
    } finally {
        _locker.unlock();
    }
    return _element;
}

From source file:net.ymate.module.oauth.client.impl.DefaultOAuthClientTokenStorageAdapter.java

@Override
public OAuthAccessToken loadAccessToken(OAuthAccount account) {
    OAuthAccessToken _accessToken = __TOKEN_CACHES.get(account.getId());
    if (_accessToken == null || _accessToken.isExpired()) {
        ReentrantLock _locker = ReentrantLockHelper.DEFAULT.getLocker(account.getId().concat("_token"));
        _locker.lock();
        try {/*w  ww.  ja v a  2 s .c om*/
            _accessToken = __doGetAccessToken(account.getId());
            if (_accessToken == null || _accessToken.isExpired()) {
                _accessToken = __doGetAccessToken(account);
            }
        } catch (Exception e) {
            try {
                _accessToken = __doGetAccessToken(account.getId());
                if (_accessToken == null || _accessToken.isExpired()) {
                    _accessToken = __doGetAccessToken(account);
                }
            } catch (Exception ex) {
                _LOG.warn("Exception when loading access_token: ", RuntimeUtils.unwrapThrow(ex));
            }
        } finally {
            if (_locker.isLocked()) {
                _locker.unlock();
            }
        }
    }
    return _accessToken;
}

From source file:RefinableHashSet.java

/**
 * Synchronize before adding, removing, or testing for item
 * @param x item involved// w  w w .  j  av  a 2  s.  co m
 */
@Override
public void acquire(T x) {
    boolean[] mark = { true };
    Thread me = Thread.currentThread();
    Thread who;
    while (true) {
        do { // wait until not resizing
            who = owner.get(mark);
        } while (mark[0] && who != me);
        ReentrantLock[] oldLocks = this.locks;
        int myBucket = Math.abs(x.hashCode() % oldLocks.length);
        ReentrantLock oldLock = oldLocks[myBucket];
        oldLock.lock(); // acquire lock
        who = owner.get(mark);
        if ((!mark[0] || who == me) && this.locks == oldLocks) { // recheck
            return;
        } else { //  unlock & try again
            oldLock.unlock();
        }
    }
}