Search in sources :

Example 16 with ReentrantLock

use of java.util.concurrent.locks.ReentrantLock in project hadoop by apache.

the class TestDomainSocketWatcher method testStress.

@Test(timeout = 300000)
public void testStress() throws Exception {
    final int SOCKET_NUM = 250;
    final ReentrantLock lock = new ReentrantLock();
    final DomainSocketWatcher watcher = newDomainSocketWatcher(10000000);
    final ArrayList<DomainSocket[]> pairs = new ArrayList<DomainSocket[]>();
    final AtomicInteger handled = new AtomicInteger(0);
    final Thread adderThread = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                for (int i = 0; i < SOCKET_NUM; i++) {
                    DomainSocket[] pair = DomainSocket.socketpair();
                    watcher.add(pair[1], new DomainSocketWatcher.Handler() {

                        @Override
                        public boolean handle(DomainSocket sock) {
                            handled.incrementAndGet();
                            return true;
                        }
                    });
                    lock.lock();
                    try {
                        pairs.add(pair);
                    } finally {
                        lock.unlock();
                    }
                }
            } catch (Throwable e) {
                LOG.error(e);
                throw new RuntimeException(e);
            }
        }
    });
    final Thread removerThread = new Thread(new Runnable() {

        @Override
        public void run() {
            final Random random = new Random();
            try {
                while (handled.get() != SOCKET_NUM) {
                    lock.lock();
                    try {
                        if (!pairs.isEmpty()) {
                            int idx = random.nextInt(pairs.size());
                            DomainSocket[] pair = pairs.remove(idx);
                            if (random.nextBoolean()) {
                                pair[0].close();
                            } else {
                                watcher.remove(pair[1]);
                            }
                        }
                    } finally {
                        lock.unlock();
                    }
                }
            } catch (Throwable e) {
                LOG.error(e);
                throw new RuntimeException(e);
            }
        }
    });
    adderThread.start();
    removerThread.start();
    Uninterruptibles.joinUninterruptibly(adderThread);
    Uninterruptibles.joinUninterruptibly(removerThread);
    watcher.close();
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) ArrayList(java.util.ArrayList) Random(java.util.Random) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 17 with ReentrantLock

use of java.util.concurrent.locks.ReentrantLock in project archaius by Netflix.

the class ConcurrentMapConfiguration method addPropertyDirect.

protected void addPropertyDirect(String key, Object value) {
    ReentrantLock lock = locks[Math.abs(key.hashCode()) % NUM_LOCKS];
    lock.lock();
    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();
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 18 with ReentrantLock

use of java.util.concurrent.locks.ReentrantLock in project mybatis-3 by mybatis.

the class BlockingCache method getLockForKey.

private ReentrantLock getLockForKey(Object key) {
    ReentrantLock lock = new ReentrantLock();
    ReentrantLock previous = locks.putIfAbsent(key, lock);
    return previous == null ? lock : previous;
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock)

Example 19 with ReentrantLock

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

the class BlockingPool method offerBatch.

private void offerBatch(List<T> offers) {
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        if (objects.size() + offers.size() <= maxSize) {
            for (T offer : offers) {
                objects.push(offer);
            }
            notEnough.signal();
        } else {
            throw new ISE("Cannot exceed pre-configured maximum size");
        }
    } finally {
        lock.unlock();
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) ISE(io.druid.java.util.common.ISE)

Example 20 with ReentrantLock

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

the class BlockingPool method poll.

private T poll(long timeout) throws InterruptedException {
    long nanos = TIME_UNIT.toNanos(timeout);
    final ReentrantLock lock = this.lock;
    lock.lockInterruptibly();
    try {
        while (objects.isEmpty()) {
            if (nanos <= 0) {
                return null;
            }
            nanos = notEnough.awaitNanos(nanos);
        }
        return objects.pop();
    } finally {
        lock.unlock();
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock)

Aggregations

ReentrantLock (java.util.concurrent.locks.ReentrantLock)1524 Lock (java.util.concurrent.locks.Lock)125 Test (org.junit.Test)78 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)74 Condition (java.util.concurrent.locks.Condition)57 ArrayList (java.util.ArrayList)45 IOException (java.io.IOException)42 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)27 File (java.io.File)22 TimeoutException (java.util.concurrent.TimeoutException)19 Before (org.junit.Before)18 AtomicReference (java.util.concurrent.atomic.AtomicReference)17 HashMap (java.util.HashMap)16 CountDownLatch (java.util.concurrent.CountDownLatch)16 ExecutionException (java.util.concurrent.ExecutionException)15 List (java.util.List)14 AtomicLong (java.util.concurrent.atomic.AtomicLong)14 Map (java.util.Map)11 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)11 Pair (android.util.Pair)10