Search in sources :

Example 1 with BooleanMutex

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();
    }
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch) BooleanMutex(com.alibaba.otter.shared.common.utils.lock.BooleanMutex) Callable(java.util.concurrent.Callable) Test(org.testng.annotations.Test) BaseOtterTest(com.alibaba.otter.shared.common.BaseOtterTest)

Example 2 with BooleanMutex

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);
    }
}
Also used : NestableRuntimeException(org.apache.commons.lang.exception.NestableRuntimeException) BooleanMutex(com.alibaba.otter.shared.common.utils.lock.BooleanMutex)

Example 3 with BooleanMutex

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();
    }
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch) BooleanMutex(com.alibaba.otter.shared.common.utils.lock.BooleanMutex) Callable(java.util.concurrent.Callable) Test(org.testng.annotations.Test) BaseOtterTest(com.alibaba.otter.shared.common.BaseOtterTest)

Example 4 with BooleanMutex

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();
    }
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch) BooleanMutex(com.alibaba.otter.shared.common.utils.lock.BooleanMutex) Callable(java.util.concurrent.Callable) Test(org.testng.annotations.Test) BaseOtterTest(com.alibaba.otter.shared.common.BaseOtterTest)

Aggregations

BooleanMutex (com.alibaba.otter.shared.common.utils.lock.BooleanMutex)4 BaseOtterTest (com.alibaba.otter.shared.common.BaseOtterTest)3 Callable (java.util.concurrent.Callable)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 ExecutorService (java.util.concurrent.ExecutorService)3 Test (org.testng.annotations.Test)3 NestableRuntimeException (org.apache.commons.lang.exception.NestableRuntimeException)1