Search in sources :

Example 1 with Lock

use of java.util.concurrent.locks.Lock in project che by eclipse.

the class PreferenceManager method update.

/**
     * Updates the preferences of the user by merging given {@code preferences}
     * with the existing preferences. If user doesn't have any preferences
     * then the given {@code preferences} will be associated with the user.
     *
     * @param userId
     *         the user whose preferences should be updated
     * @param preferences
     *         preferences update
     * @return all the user's preferences including the update
     * @throws NullPointerException
     *         when either {@code userId} or {@code preferences} is null
     * @throws ServerException
     *         when any error occurs
     */
public Map<String, String> update(String userId, Map<String, String> preferences) throws ServerException {
    requireNonNull(userId, "Required non-null user id");
    requireNonNull(preferences, "Required non-null preferences");
    // Holding reference to prevent garbage collection
    // this reentrantLock helps to avoid race-conditions when parallel updates are applied
    final Lock reentrantLock = UPDATE_REENTRANT_LOCKS.get(userId);
    reentrantLock.lock();
    try {
        final Map<String, String> found = preferenceDao.getPreferences(userId);
        found.putAll(preferences);
        preferenceDao.setPreferences(userId, found);
        return found;
    } finally {
        reentrantLock.unlock();
    }
}
Also used : Lock(java.util.concurrent.locks.Lock)

Example 2 with Lock

use of java.util.concurrent.locks.Lock in project che by eclipse.

the class PreferenceManager method remove.

/**
     * Removes the preferences with the given {@code names}.
     *
     * @param userId
     *         the id of the user to remove preferences
     * @param names
     *         the names to remove
     * @throws NullPointerException
     *         when either {@code userId} or {@code names} is null
     * @throws ServerException
     *         when any error occurs
     */
public void remove(String userId, List<String> names) throws ServerException {
    requireNonNull(userId, "Required non-null user id");
    requireNonNull(names, "Required non-null preference names");
    // Holding reference to prevent garbage collection
    // this reentrantLock helps to avoid race-conditions when parallel updates are applied
    final Lock reentrantLock = UPDATE_REENTRANT_LOCKS.get(userId);
    reentrantLock.lock();
    try {
        final Map<String, String> preferences = preferenceDao.getPreferences(userId);
        names.forEach(preferences::remove);
        preferenceDao.setPreferences(userId, preferences);
    } finally {
        reentrantLock.unlock();
    }
}
Also used : Lock(java.util.concurrent.locks.Lock)

Example 3 with Lock

use of java.util.concurrent.locks.Lock in project druid by druid-io.

the class NamespaceLookupExtractorFactory method close.

@Override
public boolean close() {
    final Lock writeLock = startStopSync.writeLock();
    try {
        writeLock.lockInterruptibly();
    } catch (InterruptedException e) {
        throw Throwables.propagate(e);
    }
    try {
        if (entry == null) {
            LOG.warn("Not started! [%s]", extractorID);
            return true;
        }
        entry.close();
        entry = null;
        return true;
    } finally {
        writeLock.unlock();
    }
}
Also used : ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Lock(java.util.concurrent.locks.Lock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock)

Example 4 with Lock

use of java.util.concurrent.locks.Lock in project druid by druid-io.

the class NamespaceLookupExtractorFactory method get.

// Grab the latest snapshot from the CacheScheduler's entry
@Override
public LookupExtractor get() {
    final Lock readLock = startStopSync.readLock();
    try {
        readLock.lockInterruptibly();
    } catch (InterruptedException e) {
        throw Throwables.propagate(e);
    }
    try {
        if (entry == null) {
            throw new ISE("Factory [%s] not started", extractorID);
        }
        final CacheScheduler.CacheState cacheState = entry.getCacheState();
        if (cacheState instanceof CacheScheduler.NoCache) {
            final String noCacheReason = ((CacheScheduler.NoCache) cacheState).name();
            throw new ISE("%s: %s, extractorID = %s", entry, noCacheReason, extractorID);
        }
        CacheScheduler.VersionedCache versionedCache = (CacheScheduler.VersionedCache) cacheState;
        Map<String, String> map = versionedCache.getCache();
        final byte[] v = StringUtils.toUtf8(versionedCache.getVersion());
        final byte[] id = StringUtils.toUtf8(extractorID);
        return new MapLookupExtractor(map, isInjective()) {

            @Override
            public byte[] getCacheKey() {
                return ByteBuffer.allocate(CLASS_CACHE_KEY.length + id.length + 1 + v.length + 1 + 1).put(CLASS_CACHE_KEY).put(id).put((byte) 0xFF).put(v).put((byte) 0xFF).put(isOneToOne() ? (byte) 1 : (byte) 0).array();
            }
        };
    } finally {
        readLock.unlock();
    }
}
Also used : ISE(io.druid.java.util.common.ISE) MapLookupExtractor(io.druid.query.extraction.MapLookupExtractor) CacheScheduler(io.druid.server.lookup.namespace.cache.CacheScheduler) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Lock(java.util.concurrent.locks.Lock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock)

Example 5 with Lock

use of java.util.concurrent.locks.Lock in project tomcat by apache.

the class Util method getExpressionFactory.

/**
     * Provides a per class loader cache of ExpressionFactory instances without
     * pinning any in memory as that could trigger a memory leak.
     */
static ExpressionFactory getExpressionFactory() {
    ClassLoader tccl = Thread.currentThread().getContextClassLoader();
    CacheValue cacheValue = null;
    ExpressionFactory factory = null;
    if (tccl == null) {
        cacheValue = nullTcclFactory;
    } else {
        CacheKey key = new CacheKey(tccl);
        cacheValue = factoryCache.get(key);
        if (cacheValue == null) {
            CacheValue newCacheValue = new CacheValue();
            cacheValue = factoryCache.putIfAbsent(key, newCacheValue);
            if (cacheValue == null) {
                cacheValue = newCacheValue;
            }
        }
    }
    final Lock readLock = cacheValue.getLock().readLock();
    readLock.lock();
    try {
        factory = cacheValue.getExpressionFactory();
    } finally {
        readLock.unlock();
    }
    if (factory == null) {
        final Lock writeLock = cacheValue.getLock().writeLock();
        writeLock.lock();
        try {
            factory = cacheValue.getExpressionFactory();
            if (factory == null) {
                factory = ExpressionFactory.newInstance();
                cacheValue.setExpressionFactory(factory);
            }
        } finally {
            writeLock.unlock();
        }
    }
    return factory;
}
Also used : ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Lock(java.util.concurrent.locks.Lock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock)

Aggregations

Lock (java.util.concurrent.locks.Lock)1341 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)371 ReadWriteLock (java.util.concurrent.locks.ReadWriteLock)328 ReentrantLock (java.util.concurrent.locks.ReentrantLock)252 Test (org.junit.Test)183 ArrayList (java.util.ArrayList)77 CountDownLatch (java.util.concurrent.CountDownLatch)69 IOException (java.io.IOException)68 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)55 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)50 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)42 HashMap (java.util.HashMap)40 Map (java.util.Map)40 Condition (java.util.concurrent.locks.Condition)32 HashSet (java.util.HashSet)28 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)27 AtomicLong (java.util.concurrent.atomic.AtomicLong)27 Ignite (org.apache.ignite.Ignite)25 Triple (org.apache.clerezza.commons.rdf.Triple)21 JID (org.xmpp.packet.JID)21