private static void executeLock() { System.out.println("### Lock Execution Started... ###"); ILock lock = context.getBean("lock", ILock.class); lock.lock(); System.out.println("lock() call..."); System.out.println("is locked by current thread: " + lock.isLockedByCurrentThread()); lock.unlock(); System.out.println("unlock() call..."); System.out.println("is locked: " + lock.isLocked()); }
@Override public boolean tryLock(long waitMillSec, long leaseMillSec) throws InterruptedException { if (waitMillSec == 0 && leaseMillSec == 0) { return lock.tryLock(); } else if (leaseMillSec == 0) { return lock.tryLock(waitMillSec, TimeUnit.MILLISECONDS); } else if (waitMillSec == 0) { return lock.tryLock(0, TimeUnit.MILLISECONDS, leaseMillSec, TimeUnit.MILLISECONDS); } else { return lock.tryLock(waitMillSec, TimeUnit.MILLISECONDS, leaseMillSec, TimeUnit.MILLISECONDS); } }
public static void main(String[] args) { HazelcastInstance hz = Hazelcast.newHazelcastInstance(); IAtomicLong counter = hz.getAtomicLong("counter"); ILock lock = hz.getLock("lock"); ICondition isOneCondition = lock.newCondition("isOne"); lock.lock(); try { counter.set(1); isOneCondition.signalAll(); } finally { lock.unlock(); } System.out.println("Value changed"); } }
@Override public boolean unLock() { try { lock.unlock(); } catch (Exception e) { return false; } return true; }
@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()); } } }
@Override public void lock() { lock.lock(); }
@Override public boolean isLocked() { return lock.isLocked(); } }
lock.lock(); nodeId(cluster), path, Thread.currentThread().getName())); Preconditions.checkState(lock.isLockedByCurrentThread()); return new LockAdapter(lock, path, cluster);
@Test public void testLock() throws Exception { HazelcastLockRegistry registry = new HazelcastLockRegistry(instance); for (int i = 0; i < 10; i++) { Lock lock = registry.obtain("foo"); lock.lock(); try { assertTrue(((ILock) lock).isLocked()); assertTrue(((ILock) lock).isLockedByCurrentThread()); } finally { lock.unlock(); } } }
@Override public void delete() { lock.forceUnlock(); }
@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.warning(message); } else { fail(message); } } if (remainingLeaseTime > 0) { fail(format("%s has remainingLeaseTime: %d ms", lock, remainingLeaseTime)); } } } }
@ManagedAnnotation("remainingLeaseTime") @ManagedDescription("remaining time in milliseconds or -1 if not locked") public long getRemainingLeaseTime() { return managedObject.getRemainingLeaseTime(); }
public static void main(String[] args) throws Exception { HazelcastInstance hz = Hazelcast.newHazelcastInstance(); IAtomicLong counter = hz.getAtomicLong("counter"); ILock lock = hz.getLock("lock"); ICondition isOneCondition = lock.newCondition("isOne"); lock.lock(); try { while (counter.get() != 1) { System.out.println("Waiting"); isOneCondition.await(); } } finally { lock.unlock(); } System.out.println("Wait finished, counter: " + counter.get()); } }
@Override public boolean unLock() { try { lock.unlock(); } catch (Exception e) { return false; } return true; }
private void loadSegment(int segmentNumber) throws Exception { indexLock.writeLock().lock(); try { if (!segmentMap.containsKey(segmentNumber)) { String lockName = indexName + "-" + segmentNumber; ILock hzLock = hazelcastManager.getLock(lockName); hazelLockMap.put(segmentNumber, hzLock); log.info("Waiting for lock for index <" + indexName + "> segment <" + segmentNumber + ">"); hzLock.lock(); log.info("Obtained lock for index <" + indexName + "> segment <" + segmentNumber + ">"); //Just for clarity IndexSegmentInterface indexSegmentInterface = this; //doesnt need to be done each time and it is done in StartNode but helps with test cases that take different paths FacetsConfig.DEFAULT_DIM_CONFIG.multiValued = true; facetsConfig = new FacetsConfig(); LumongoSegment s = new LumongoSegment(segmentNumber, indexSegmentInterface, indexConfig, facetsConfig, documentStorage); segmentMap.put(segmentNumber, s); log.info("Loaded segment <" + segmentNumber + "> for index <" + indexName + ">"); log.info("Current segments <" + (new TreeSet<>(segmentMap.keySet())) + "> for index <" + indexName + ">"); } } finally { indexLock.writeLock().unlock(); } }