HazelcastInstance instance = createHazelcastInstance();
String lockName = randomString();
String conditionName = randomString();
final ILock lock = instance.getLock(lockName);
final ICondition condition = lock.newCondition(conditionName);
final AtomicInteger count = new AtomicInteger(0);
final int k = 50;
final CountDownLatch awaitLatch = new CountDownLatch(k);
final CountDownLatch finalLatch = new CountDownLatch(k);
for (int i = 0; i < k; i++) {
new Thread(new Runnable() {
public void run() {
try {
lock.lock();
if (lock.isLockedByCurrentThread()) {
count.incrementAndGet();
}
awaitLatch.countDown();
condition.await();
if (lock.isLockedByCurrentThread()) {
count.incrementAndGet();
}
} catch (InterruptedException ignored) {
} finally {
lock.unlock();
finalLatch.countDown();
}
}
}).start();
}
awaitLatch.await(1, TimeUnit.MINUTES);
lock.lock();
condition.signalAll();
lock.unlock();
finalLatch.await(1, TimeUnit.MINUTES);
assertEquals(k * 2, count.get());
}