当前位置: 首页>>代码示例>>Java>>正文


Java ILock类代码示例

本文整理汇总了Java中com.hazelcast.core.ILock的典型用法代码示例。如果您正苦于以下问题:Java ILock类的具体用法?Java ILock怎么用?Java ILock使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


ILock类属于com.hazelcast.core包,在下文中一共展示了ILock类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: disposeLock

import com.hazelcast.core.ILock; //导入依赖的package包/类
public void disposeLock(Lock lock) {
	switch (Settings.INSTANCE.clusteringMode()) {
	case HAZELCAST:
		((ILock) lock).destroy();
		break;

	case IGNITE:
		break;

	case SINGLE:
		break;

	default:
		break;
	}
}
 
开发者ID:anyflow,项目名称:lannister,代码行数:17,代码来源:ClusterDataDisposer.java

示例2: synchronizePut

import com.hazelcast.core.ILock; //导入依赖的package包/类
public Object synchronizePut(Object key, Object value, String map) {
 if(isRunning()){
    ILock lock = hazelcast.getLock(map);
    
    try
    {
      if(lock.tryLock(10, TimeUnit.SECONDS))
      {
        return hazelcast.getMap(map).put(key, value);
      }
      else
      {
        log.warn("[synchronizePut] Operation did not synchroznize in 10 secs");
        return hazelcast.getMap(map).put(key, value);
        
      }
    } catch (InterruptedException e) {
      Thread.currentThread().interrupt();
      log.debug("", e);
    }
    finally
    {
      lock.unlock();
    }
    
  }
  return null;
  
}
 
开发者ID:javanotes,项目名称:reactive-data,代码行数:30,代码来源:HazelcastInstanceProxy.java

示例3: doRunInitialization

import com.hazelcast.core.ILock; //导入依赖的package包/类
@Override
protected void doRunInitialization(final String beanName) {
    LockKey lockKey = new LockKey(KeyType.INITIALIZATION, beanName);
    // Try to get the initialization lock
    ILock lock = hazelcastInstance.getLock(lockKey.toString());
    if (lock.tryLock()) {
        // No one else is trying to run this initialization right now. Check if it was already ran by someone else
        if (!initializationControl.containsKey(beanName)) {
            try {
                // This initialization was never executed. Run it and mark it as executed
                super.doRunInitialization(beanName);
                initializationControl.put(beanName, beanName);
            } finally {
                HazelcastHelper.release(lock);
            }
        }
    } else {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Not running initialization for bean " + beanName + " because some other node is currently running it");
        }
    }
}
 
开发者ID:mateli,项目名称:OpenCyclos,代码行数:23,代码来源:HazelcastTaskRunner.java

示例4: doRunPollingTask

import com.hazelcast.core.ILock; //导入依赖的package包/类
@Override
protected boolean doRunPollingTask(final String key, final Callable<Boolean> task) {
    LockKey lockKey = new LockKey(KeyType.POLLING_TASK, key);
    ILock lock = hazelcastInstance.getLock(lockKey.toString());
    // Ensure multiple nodes can't run a polling task simultaneously
    if (lock.tryLock()) {
        try {
            return super.doRunPollingTask(key, task);
        } finally {
            HazelcastHelper.release(lock);
        }
    } else {
        // Force a sleep, as couldn't get the lock for this polling task
        if (LOG.isDebugEnabled()) {
            LOG.debug("Some other cluster node is running the " + key + " polling task. Leaving.");
        }
        return false;
    }
}
 
开发者ID:mateli,项目名称:OpenCyclos,代码行数:20,代码来源:HazelcastTaskRunner.java

示例5: acquire

import com.hazelcast.core.ILock; //导入依赖的package包/类
private void acquire(final LockKey key) {
    if (acquiredLocks.containsKey(key)) {
        // Already own the lock
        return;
    }
    ILock lock = hazelcastInstance.getLock(key.toString());
    try {
        if (lock.tryLock(timeoutSeconds, TimeUnit.SECONDS)) {
            acquiredLocks.put(key, lock);
        } else {
            throw new LockingException();
        }
    } catch (InterruptedException e) {
        throw new LockingException(e);
    }

}
 
开发者ID:mateli,项目名称:OpenCyclos,代码行数:18,代码来源:HazelcastLockHandlerFactory.java

示例6: testLockUnlock

import com.hazelcast.core.ILock; //导入依赖的package包/类
@Test
public void testLockUnlock() throws InterruptedException {
    HazelcastClient hClient = getHazelcastClient();
    final ILock lock = hClient.getLock("testLockUnlock");
    lock.lock();
    final CountDownLatch latch = new CountDownLatch(1);
    final CountDownLatch unlockLatch = new CountDownLatch(1);
    new Thread(new Runnable() {
        public void run() {
            assertFalse(lock.tryLock());
            unlockLatch.countDown();
            lock.lock();
            latch.countDown();
        }
    }).start();
    assertTrue(unlockLatch.await(10, TimeUnit.SECONDS));
    lock.unlock();
    assertTrue(latch.await(10, TimeUnit.SECONDS));
}
 
开发者ID:mdogan,项目名称:hazelcast-archive,代码行数:20,代码来源:HazelcastClientLockTest.java

示例7: testTryLock

import com.hazelcast.core.ILock; //导入依赖的package包/类
@Test
public void testTryLock() throws InterruptedException {
    HazelcastClient hClient = getHazelcastClient();
    final ILock lock = hClient.getLock("testTryLock");
    assertTrue(lock.tryLock());
    lock.lock();
    final CountDownLatch latch = new CountDownLatch(1);
    final CountDownLatch unlockLatch = new CountDownLatch(1);
    new Thread(new Runnable() {

        public void run() {
            assertFalse(lock.tryLock());
            unlockLatch.countDown();
            try {
                assertTrue(lock.tryLock(10, TimeUnit.SECONDS));
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            latch.countDown();
        }
    }).start();
    assertTrue(unlockLatch.await(10, TimeUnit.SECONDS));
    lock.unlock();
    lock.unlock();
    assertTrue(latch.await(10, TimeUnit.SECONDS));
}
 
开发者ID:mdogan,项目名称:hazelcast-archive,代码行数:27,代码来源:HazelcastClientLockTest.java

示例8: acquireDistributedLock

import com.hazelcast.core.ILock; //导入依赖的package包/类
protected com.hazelcast.core.ILock acquireDistributedLock(Object object) {
    if (object == null) {
        if (log.isWarnEnabled()) {
            log.warn("Could not acquire distributed lock, object is null");
        }
        return null;
    }

    if (log.isDebugEnabled()) {
        log.debug(String.format("Acquiring distributed lock for %s...", object.getClass().getSimpleName()));
    }
    ILock lock = getHazelcastInstance().getLock(object);
    if (log.isDebugEnabled()) {
        log.debug(String.format("Distributed lock acquired for %s", object.getClass().getSimpleName()));
    }
    return lock;
}
 
开发者ID:apache,项目名称:stratos,代码行数:18,代码来源:HazelcastDistributedObjectProvider.java

示例9: releaseDistributedLock

import com.hazelcast.core.ILock; //导入依赖的package包/类
protected void releaseDistributedLock(ILock lock) {
    if (lock == null) {
        if (log.isWarnEnabled()) {
            log.warn("Could not release distributed lock, lock is null");
        }
        return;
    }

    if (log.isDebugEnabled()) {
        log.debug(String.format("Releasing distributed lock for %s...", lock.getKey()));
    }
    lock.forceUnlock();
    if (log.isDebugEnabled()) {
        log.debug(String.format("Distributed lock released for %s", lock.getKey()));
    }
}
 
开发者ID:apache,项目名称:stratos,代码行数:17,代码来源:HazelcastDistributedObjectProvider.java

示例10: verify

import com.hazelcast.core.ILock; //导入依赖的package包/类
@Verify(global = true)
public void verify() {
    for (int i = 0; i < maxAccounts; i++) {
        ILock lock = targetInstance.getLock(name + i);
        assertFalse(name + ": Lock should be unlocked", lock.isLocked());
    }

    long totalValue = 0;
    IList<Long> accounts = targetInstance.getList(name);
    for (long value : accounts) {
        totalValue += value;
    }
    logger.info(": totalValue=" + totalValue);
    assertEquals(name + ": totalInitialValue != totalValue ", totalInitialValue, totalValue);

    Counter total = new Counter();
    IList<Counter> totals = targetInstance.getList(name + "count");
    for (Counter count : totals) {
        total.add(count);
    }
    logger.info("total count " + total);
}
 
开发者ID:hazelcast,项目名称:hazelcast-simulator,代码行数:23,代码来源:TryLockTimeOutTest.java

示例11: timeStep

import com.hazelcast.core.ILock; //导入依赖的package包/类
@TimeStep
public void timeStep(BaseThreadState state) {
    int lockIndex = state.randomInt(lockCount);
    ILock lock = targetInstance.getLock(name + lockIndex);

    int leaseTime = 1 + state.randomInt(maxLeaseTimeMillis);
    int tryTime = 1 + state.randomInt(maxTryTimeMillis);

    if (state.randomBoolean()) {
        lock.lock(leaseTime, MILLISECONDS);
    } else {
        try {
            lock.tryLock(tryTime, MILLISECONDS, leaseTime, MILLISECONDS);
        } catch (InterruptedException e) {
            logger.info("tryLock() got exception: " + e.getMessage());
        }
    }
}
 
开发者ID:hazelcast,项目名称:hazelcast-simulator,代码行数:19,代码来源:LeaseLockTest.java

示例12: verify

import com.hazelcast.core.ILock; //导入依赖的package包/类
@Verify
public void verify() {
    for (int i = 0; i < lockCount; i++) {
        ILock lock = targetInstance.getLock(name + i);

        boolean isLocked = lock.isLocked();
        long remainingLeaseTime = lock.getRemainingLeaseTime();
        if (isLocked) {
            String message = format("%s is locked with remainingLeaseTime: %d ms", lock, remainingLeaseTime);
            if (allowZeroMillisRemainingLeaseLockTime && remainingLeaseTime == 0) {
                logger.warn(message);
            } else {
                fail(message);
            }
        }
        if (remainingLeaseTime > 0) {
            fail(format("%s has remainingLeaseTime: %d ms", lock, remainingLeaseTime));
        }
    }
}
 
开发者ID:hazelcast,项目名称:hazelcast-simulator,代码行数:21,代码来源:LeaseLockTest.java

示例13: doRunInitialization

import com.hazelcast.core.ILock; //导入依赖的package包/类
@Override
protected void doRunInitialization(final String beanName) {
    LockKey lockKey = new LockKey(KeyType.INITIALIZATION, beanName);
    // Try to get the initialization lock
    ILock lock = hazelcastInstance.getLock(lockKey);
    if (lock.tryLock()) {
        // No one else is trying to run this initialization right now. Check if it was already ran by someone else
        if (!initializationControl.containsKey(beanName)) {
            try {
                // This initialization was never executed. Run it and mark it as executed
                super.doRunInitialization(beanName);
                initializationControl.put(beanName, beanName);
            } finally {
                HazelcastHelper.release(lock);
            }
        }
    } else {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Not running initialization for bean " + beanName + " because some other node is currently running it");
        }
    }
}
 
开发者ID:crypto-coder,项目名称:open-cyclos,代码行数:23,代码来源:HazelcastTaskRunner.java

示例14: doRunPollingTask

import com.hazelcast.core.ILock; //导入依赖的package包/类
@Override
protected boolean doRunPollingTask(final String key, final Callable<Boolean> task) {
    LockKey lockKey = new LockKey(KeyType.POLLING_TASK, key);
    ILock lock = hazelcastInstance.getLock(lockKey);
    // Ensure multiple nodes can't run a polling task simultaneously
    if (lock.tryLock()) {
        try {
            return super.doRunPollingTask(key, task);
        } finally {
            HazelcastHelper.release(lock);
        }
    } else {
        // Force a sleep, as couldn't get the lock for this polling task
        if (LOG.isDebugEnabled()) {
            LOG.debug("Some other cluster node is running the " + key + " polling task. Leaving.");
        }
        return false;
    }
}
 
开发者ID:crypto-coder,项目名称:open-cyclos,代码行数:20,代码来源:HazelcastTaskRunner.java

示例15: acquire

import com.hazelcast.core.ILock; //导入依赖的package包/类
private void acquire(final LockKey key) {
    if (acquiredLocks.containsKey(key)) {
        // Already own the lock
        return;
    }
    ILock lock = hazelcastInstance.getLock(key);
    try {
        if (lock.tryLock(timeoutSeconds, TimeUnit.SECONDS)) {
            acquiredLocks.put(key, lock);
        } else {
            throw new LockingException();
        }
    } catch (InterruptedException e) {
        throw new LockingException(e);
    }

}
 
开发者ID:crypto-coder,项目名称:open-cyclos,代码行数:18,代码来源:HazelcastLockHandlerFactory.java


注:本文中的com.hazelcast.core.ILock类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。