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:edu.brown.cs.systems.retro.throttling.throttlingqueues.ThrottlingLockingQueue.java

@Override
public void clearRates() {
    final ReentrantLock lock = this.lock;
    lock.lock();/*from   w  w w  .  java 2s.c o m*/
    try {
        for (TenantQueue q : qs.values()) {
            q.clearRate();
        }
    } finally {
        lock.unlock();
    }
}

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();/*  ww  w  . j  a  va  2  s.  c  o  m*/
        try {
            _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:com.googlecode.msidor.springframework.integration.channel.ConcurentOrderedMultiQueueChannel.java

/**
 * Unlock the execution ID.//from  w w w.  ja v  a2  s.  c om
 * This method lock the object to perform manipulation on locks collection and to notify dormant consumer threads that there are potential messages to check.
 * 
 * @param message to unlock
 */
public void unlockExecutionId(Message<?> message) {
    final ReentrantLock lock = this.objectLock;

    try {
        lock.lockInterruptibly();

        Object executionId = getExecutionIdFromMessage(message);

        log.trace("Unlocking execution " + executionId);

        if (executionId != null) {
            executionsLocks.remove(executionId);
        }
    } catch (InterruptedException e) {
        log.trace("Lock interrupted by other thread");
    } finally {
        newMessagesToCheck.signal();
        lock.unlock();
    }
}

From source file:com.wooki.domain.biz.ChapterManagerImpl.java

public void updateContent(Long chapterId, Draft draft) {

    assert chapterId != null;
    assert draft != null;
    assert draft.getData() != null;

    // Update last modified timestamp
    Date lastModified = Calendar.getInstance().getTime();
    ReentrantLock lock = getOrCreateLock(chapterId);

    Publication publication = publicationDao.findLastRevision(chapterId);
    lock.lock();/*from  www . ja v a2  s.  c  om*/

    Chapter chapter = null;

    try {
        chapter = chapterDao.findById(chapterId);
        if (chapter.getLastModified() != null && !chapter.getLastModified().equals(draft.getTimestamp())) {
            throw new ConcurrentModificationException(
                    "Document has been modified by another user in the meantime.");
        }
        chapter.setLastModified(lastModified);
        chapterDao.update(chapter);
    } finally {
        lock.unlock();
    }

    // we check the published flag. If set, then this Publication must
    // be considered as "locked" and we must create a new publication as
    // the new working copy
    if (publication == null || (publication != null && publication.isPublished())) {
        publication = new Publication();

        // Security check
        if (!securityCtx.isLoggedIn() || !this.securityCtx.canWrite(chapter.getBook())) {
            throw new AuthorizationException("Publish action not authorized");
        }
        publication.setChapter(chapter);

        publication.setCreationDate(Calendar.getInstance().getTime());
        publicationDao.create(publication);
    }

    publication.setContent(draft.getData());
    publication.setLastModified(lastModified);
    publicationDao.update(publication);

}

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.
 */// w w  w  .j a  v  a  2s .  c o  m
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:com.netflix.config.ConcurrentMapConfiguration.java

protected void addPropertyDirect(String key, Object value) {
    ReentrantLock lock = locks[Math.abs(key.hashCode()) % NUM_LOCKS];
    lock.lock();/*from   w w w.j  a v a2 s . c  o  m*/
    try {
        Object previousValue = map.putIfAbsent(key, value);
        if (previousValue == null) {
            return;
        }
        if (previousValue instanceof List) {
            // the value is added to the existing list
            ((List) previousValue).add(value);
        } else {
            // the previous value is replaced by a list containing the previous value and the new value
            List<Object> list = new CopyOnWriteArrayList<Object>();
            list.add(previousValue);
            list.add(value);
            map.put(key, list);
        }
    } finally {
        lock.unlock();
    }
}

From source file:org.gdg.frisbee.android.cache.ModelCache.java

public CacheItem put(final String url, final Object obj, DateTime expiresAt) {

    if (obj == null)
        return null;

    Log.d(LOG_TAG, String.format("put(%s)", url));
    CacheItem d = new CacheItem(obj, expiresAt);

    if (null != mMemoryCache) {
        mMemoryCache.put(url, d);//from ww  w  .j ava  2s . c  o m
    }

    if (null != mDiskCache) {
        checkNotOnMainThread();

        final String key = transformUrlForDiskCacheKey(url);
        final ReentrantLock lock = getLockForDiskCacheEdit(key);
        lock.lock();
        try {
            DiskLruCache.Editor editor = mDiskCache.edit(key);
            writeValueToDisk(editor.newOutputStream(0), obj);
            writeExpirationToDisk(editor.newOutputStream(1), expiresAt);
            editor.commit();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
            scheduleDiskCacheFlush();
        }
    }

    return d;
}

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

/**
 * Inserts the specified element into this delay queue.
 *
 * @param e/*from ww w  .  j  ava2s  .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: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  va 2 s.c  o  m*/
 * 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 w w .ja  v  a  2s  . co m*/
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);
    }
}