Package com.hazelcast.core

Examples of com.hazelcast.core.ICondition


    public void test() {
        HazelcastInstance[] instances = createHazelcastInstanceFactory(INSTANCE_COUNT).newInstances();
        HazelcastInstance hz = instances[0];
        //Hazelcast.newHazelcastInstance();
        ILock lock = hz.getLock(randomString());
        ICondition condition = lock.newCondition(randomString());

        ConsumerThread[] consumers = new ConsumerThread[CONSUMER_COUNT];
        for (int k = 0; k < consumers.length; k++) {
            ConsumerThread thread = new ConsumerThread(1, lock, condition);
            thread.start();
View Full Code Here


    @Test(timeout = 60000)
    public void testMultipleConditionsForSameLock() throws InterruptedException {
        HazelcastInstance instance = createHazelcastInstance();

        final ILock lock = instance.getLock(randomString());
        final ICondition condition0 = lock.newCondition(randomString());
        final ICondition condition1 = lock.newCondition(randomString());

        final CountDownLatch latch = new CountDownLatch(2);
        final CountDownLatch syncLatch = new CountDownLatch(2);

        createThreadWaitsForCondition(latch, lock, condition0, syncLatch).start();
        createThreadWaitsForCondition(latch, lock, condition1, syncLatch).start();

        syncLatch.await();

        lock.lock();
        condition0.signal();
        lock.unlock();

        lock.lock();
        condition1.signal();
        lock.unlock();

        assertOpenEventually(latch);
    }
View Full Code Here

    @Test(timeout = 60000)
    public void testSignalAll() throws InterruptedException {
        HazelcastInstance instance = createHazelcastInstance();

        final ILock lock = instance.getLock(randomString());
        final ICondition condition = lock.newCondition(randomString());

        final CountDownLatch latch = new CountDownLatch(2);
        final CountDownLatch syncLatch = new CountDownLatch(2);

        createThreadWaitsForCondition(latch, lock, condition, syncLatch).start();
        createThreadWaitsForCondition(latch, lock, condition, syncLatch).start();

        syncLatch.await();

        lock.lock();
        condition.signalAll();
        lock.unlock();

        assertOpenEventually(latch);
    }
View Full Code Here

    @Test(timeout = 60000)
    public void testSignalAll_whenMultipleConditions() throws InterruptedException {
        HazelcastInstance instance = createHazelcastInstance();

        final ILock lock = instance.getLock(randomString());
        final ICondition condition0 = lock.newCondition(randomString());
        final ICondition condition1 = lock.newCondition(randomString());

        final CountDownLatch latch = new CountDownLatch(10);
        final CountDownLatch syncLatch = new CountDownLatch(2);

        createThreadWaitsForCondition(latch, lock, condition0, syncLatch).start();
View Full Code Here

    public void testSameConditionRetrievedMultipleTimesForSameLock() throws InterruptedException {
        HazelcastInstance instance = createHazelcastInstance();

        final ILock lock = instance.getLock(randomString());
        String name = randomString();
        final ICondition condition0 = lock.newCondition(name);
        final ICondition condition1 = lock.newCondition(name);

        final CountDownLatch latch = new CountDownLatch(2);
        final CountDownLatch syncLatch = new CountDownLatch(2);

        createThreadWaitsForCondition(latch, lock, condition0, syncLatch).start();
View Full Code Here

    @Test
    public void testAwaitTime_whenTimeout() throws InterruptedException {
       HazelcastInstance instance = createHazelcastInstance();

        final ILock lock = instance.getLock(randomString());
        final ICondition condition = lock.newCondition(randomString());
        lock.lock();

        boolean success = condition.await(1, TimeUnit.MILLISECONDS);

        assertFalse(success);
        assertTrue(lock.isLockedByCurrentThread());
    }
View Full Code Here

    public void testConditionsWithSameNameButDifferentLocksAreIndependent() throws InterruptedException {
        HazelcastInstance instance = createHazelcastInstance();

        String name = randomString();
        ILock lock0 = instance.getLock(randomString());
        final ICondition condition0 = lock0.newCondition(name);
        ILock lock1 = instance.getLock(randomString());
        final ICondition condition1 = lock1.newCondition(name);

        final CountDownLatch latch = new CountDownLatch(2);
        final CountDownLatch syncLatch = new CountDownLatch(2);

        createThreadWaitsForCondition(latch, lock0, condition0, syncLatch).start();

        createThreadWaitsForCondition(latch, lock1, condition1, syncLatch).start();

        syncLatch.await();

        lock0.lock();
        condition0.signalAll();
        lock0.unlock();

        lock1.lock();
        condition1.signalAll();
        lock1.unlock();

        assertOpenEventually(latch);
    }
View Full Code Here

        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);

        Thread t = new Thread(new Runnable() {
            public void run() {
                try {
                    lock.lock();
                    if (lock.isLockedByCurrentThread()) {
                        count.incrementAndGet();
                    }
                    condition.await();
                    if (lock.isLockedByCurrentThread()) {
                        count.incrementAndGet();
                    }
                } catch (InterruptedException ignored) {
                } finally {
                    lock.unlock();
                }
            }
        });
        t.start();
        Thread.sleep(1000);

        assertEquals(false, lock.isLocked());
        lock.lock();
        assertEquals(true, lock.isLocked());
        condition.signal();
        lock.unlock();
        t.join();
        assertEquals(2, count.get());
    }
View Full Code Here

        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());
    }
View Full Code Here

        HazelcastInstance instance = createHazelcastInstance();

        String lockName = randomString();
        String conditionName = randomString();
        final ILock lock = instance.getLock(lockName);
        final ICondition condition = lock.newCondition(conditionName);

        final AtomicBoolean running = new AtomicBoolean(true);
        final AtomicReference<Exception> errorRef = new AtomicReference<Exception>();
        final int numberOfThreads = 8;
        final CountDownLatch finalLatch = new CountDownLatch(numberOfThreads);
        ExecutorService ex = Executors.newCachedThreadPool();

        for (int i = 0; i < numberOfThreads; i++) {
            ex.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        while (running.get()) {
                            lock.lock();
                            try {
                                condition.await(1, TimeUnit.MILLISECONDS);
                            } catch (InterruptedException ignored) {
                            } catch (IllegalStateException e) {
                                errorRef.set(e);
                                running.set(false);
                            } finally {
View Full Code Here

TOP

Related Classes of com.hazelcast.core.ICondition

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.