use of com.alibaba.otter.shared.common.utils.lock.BooleanMutex in project otter by alibaba.
the class BooleanMutexTest method test_concurrent_true.
@Test
public void test_concurrent_true() {
try {
final BooleanMutex mutex = new BooleanMutex(true);
final CountDownLatch count = new CountDownLatch(10);
ExecutorService executor = Executors.newCachedThreadPool();
for (int i = 0; i < 10; i++) {
executor.submit(new Callable() {
public Object call() throws Exception {
mutex.get();
count.countDown();
return null;
}
});
}
count.await();
executor.shutdown();
} catch (InterruptedException e) {
want.fail();
}
}
use of com.alibaba.otter.shared.common.utils.lock.BooleanMutex in project otter by alibaba.
the class DistributedLock method lock.
/**
* 尝试获取锁操作,阻塞式可被中断
*/
public void lock() throws InterruptedException, KeeperException {
// 可能初始化的时候就失败了
if (exception != null) {
throw exception;
}
if (interrupt != null) {
throw interrupt;
}
if (other != null) {
throw new NestableRuntimeException(other);
}
if (isOwner()) {
// 锁重入
return;
}
BooleanMutex mutex = new BooleanMutex();
acquireLock(mutex);
mutex.get();
if (exception != null) {
unlock();
throw exception;
}
if (interrupt != null) {
unlock();
throw interrupt;
}
if (other != null) {
unlock();
throw new NestableRuntimeException(other);
}
}
use of com.alibaba.otter.shared.common.utils.lock.BooleanMutex in project otter by alibaba.
the class BooleanMutexTest method test_init_false.
@Test
public void test_init_false() {
final BooleanMutex mutex = new BooleanMutex(false);
try {
final CountDownLatch count = new CountDownLatch(1);
ExecutorService executor = Executors.newCachedThreadPool();
executor.submit(new Callable() {
public Object call() throws Exception {
Thread.sleep(1000);
mutex.set(true);
count.countDown();
return null;
}
});
// 会被阻塞,等异步线程释放锁对象
mutex.get();
count.await();
executor.shutdown();
} catch (InterruptedException e) {
want.fail();
}
}
use of com.alibaba.otter.shared.common.utils.lock.BooleanMutex in project otter by alibaba.
the class BooleanMutexTest method test_concurrent_false.
@Test
public void test_concurrent_false() {
try {
// 初始为false
final BooleanMutex mutex = new BooleanMutex(false);
final CountDownLatch count = new CountDownLatch(10);
ExecutorService executor = Executors.newCachedThreadPool();
for (int i = 0; i < 10; i++) {
executor.submit(new Callable() {
public Object call() throws Exception {
// 被阻塞
mutex.get();
count.countDown();
return null;
}
});
}
Thread.sleep(1000);
mutex.set(true);
count.await();
executor.shutdown();
} catch (InterruptedException e) {
want.fail();
}
}
Aggregations