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();
}
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();
}
}
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;
}
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();
}
}
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();
}
}
Aggregations