Пример #1
0
  @Test
  public void testLockConditionSignalAllShutDownKeyOwner() throws InterruptedException {
    final TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(2);
    final Config config = new Config();
    final String name = "testLockConditionSignalAllShutDownKeyOwner";
    final HazelcastInstance instance = nodeFactory.newHazelcastInstance(config);
    final AtomicInteger count = new AtomicInteger(0);
    final int size = 50;
    int k = 0;
    final HazelcastInstance keyOwner = nodeFactory.newHazelcastInstance(config);
    while (!keyOwner
        .getCluster()
        .getLocalMember()
        .equals(instance.getPartitionService().getPartition(++k).getOwner())) {
      Thread.sleep(10);
    }

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

    final CountDownLatch awaitLatch = new CountDownLatch(size);
    final CountDownLatch finalLatch = new CountDownLatch(size);
    for (int i = 0; i < size; i++) {
      new Thread(
              new Runnable() {
                public void run() {
                  lock.lock();
                  try {
                    awaitLatch.countDown();
                    condition.await();
                    Thread.sleep(5);
                    if (lock.isLockedByCurrentThread()) {
                      count.incrementAndGet();
                    }
                  } catch (InterruptedException ignored) {
                  } finally {
                    lock.unlock();
                    finalLatch.countDown();
                  }
                }
              })
          .start();
    }

    final ILock lock1 = keyOwner.getLock(k);
    final ICondition condition1 = lock1.newCondition(name);
    awaitLatch.await(1, TimeUnit.MINUTES);
    lock1.lock();
    condition1.signalAll();
    lock1.unlock();
    keyOwner.getLifecycleService().shutdown();

    finalLatch.await(2, TimeUnit.MINUTES);
    Assert.assertEquals(size, count.get());
  }
Пример #2
0
  @Test
  public void testLockConditionSignalAll() throws InterruptedException {
    final TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(2);
    final Config config = new Config();
    final String name = "testLockConditionSimpleUsage";
    final ILock lock = nodeFactory.newHazelcastInstance(config).getLock(name);
    final ICondition condition = lock.newCondition(name + "c");
    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);
    final ILock lock1 = nodeFactory.newHazelcastInstance(config).getLock(name);
    final ICondition condition1 = lock1.newCondition(name + "c");
    lock1.lock();
    condition1.signalAll();
    lock1.unlock();
    finalLatch.await(1, TimeUnit.MINUTES);
    Assert.assertEquals(k * 2, count.get());
  }