com.hazelcast.core.IMap.lock()方法的使用及代码示例

x33g5p2x  于2022-01-20 转载在 其他  
字(6.3k)|赞(0)|评价(0)|浏览(211)

本文整理了Java中com.hazelcast.core.IMap.lock()方法的一些代码示例,展示了IMap.lock()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。IMap.lock()方法的具体详情如下:
包路径:com.hazelcast.core.IMap
类名称:IMap
方法名:lock

IMap.lock介绍

[英]Acquires the lock for the specified key.

If the lock is not available, then the current thread becomes disabled for thread scheduling purposes and lies dormant until the lock has been acquired.

You get a lock whether the value is present in the map or not. Other threads (possibly on other systems) would block on their invoke of lock() until the non-existent key is unlocked. If the lock holder introduces the key to the map, the put() operation is not blocked. If a thread not holding a lock on the non-existent key tries to introduce the key while a lock exists on the non-existent key, the put() operation blocks until it is unlocked.

Scope of the lock is this map only. Acquired lock is only for the key in this map.

Locks are re-entrant so if the key is locked N times then it should be unlocked N times before another thread can acquire it.

There is no lock timeout on this method. Locks will be held infinitely.

Warning:

This method uses hashCode and equals of the binary form of the key, not the actual implementations of hashCode and equalsdefined in the key's class.
[中]获取指定密钥的锁。
如果锁不可用,则出于线程调度目的,当前线程将被禁用,并处于休眠状态,直到获得锁为止。
无论值是否存在于映射中,都会得到一个锁。其他线程(可能在其他系统上)会阻塞对lock()的调用,直到不存在的密钥被解锁。如果锁持有者将钥匙引入地图,则put()操作不会被阻止。如果在不存在的密钥上存在锁时,没有在不存在的密钥上持有锁的线程尝试引入该密钥,则put()操作将阻塞,直到其解锁。
锁的范围仅限于此地图。获取的锁仅用于此地图中的钥匙。
锁是可重入的,因此如果密钥被锁定N次,那么在另一个线程获取它之前,它应该被解锁N次。
此方法上没有锁定超时。锁将被无限持有。
警告:
此方法使用密钥二进制形式的hashCode和equals,而不是密钥类中定义的hashCode和equals的实际实现。

代码示例

代码示例来源:origin: hazelcast/hazelcast-jet

@Override
public void lock(K key) {
  map.lock(key);
}

代码示例来源:origin: hazelcast/hazelcast-jet

@Override
public void lock(K key, long leaseTime, TimeUnit timeUnit) {
  map.lock(key, leaseTime, timeUnit);
}

代码示例来源:origin: hazelcast/hazelcast-code-samples

public void run() {
    IMap map = hazelcast.getMap("myMap");
    int key = random.nextInt(SIZE);
    map.lock(key);
    try {
      Thread.sleep(1);
    } catch (InterruptedException e) {
      throw new RuntimeException(e);
    } finally {
      map.unlock(key);
    }
  }
}, 1);

代码示例来源:origin: org.apache.camel/camel-hazelcast

/**
 * UPDATE an object in your cache (the whole object will be replaced)
 */
private void update(Object oid, Exchange exchange) {
  Object body = exchange.getIn().getBody();
  this.cache.lock(oid);
  this.cache.replace(oid, body);
  this.cache.unlock(oid);
}

代码示例来源:origin: org.apache.camel/camel-hazelcast

/**
 * Replaces the entry for given id with a specific value in the body, only if currently mapped to a given value
 */
private void update(Object oid, Object ovalue, Exchange exchange) {
  Object body = exchange.getIn().getBody();
  this.cache.lock(oid);
  this.cache.replace(oid, ovalue, body);
  this.cache.unlock(oid);
}

代码示例来源:origin: org.meridor.perspective/perspective-backend-common

@Override
public <K, T> void modifyMap(String mapId, K key, Consumer<Map<K, T>> action) {
  IMap<K, T> map = getMap(mapId);
  map.lock(key, lockTimeout, TimeUnit.MILLISECONDS);
  try {
    action.accept(map);
  } finally {
    map.unlock(key);
  }
}

代码示例来源:origin: org.meridor.perspective/perspective-backend-common

@Override
public <K, I, O> O readFromMap(String mapId, K key, Function<Map<K, I>, O> function) {
  IMap<K, I> map = getMap(mapId);
  map.lock(key, lockTimeout, TimeUnit.MILLISECONDS);
  try {
    return function.apply(map);
  } finally {
    map.unlock(key);
  }
}

代码示例来源:origin: org.apache.camel/camel-hazelcast

@Override
public boolean contains(String key) {
  repo.lock(key);
  try {
    return this.repo.containsKey(key);
  } finally {
    repo.unlock(key);
  }
}

代码示例来源:origin: org.apache.camel/camel-hazelcast

@Override
public boolean add(String key) {
  repo.lock(key);
  try {
    return repo.putIfAbsent(key, false) == null;
  } finally {
    repo.unlock(key);
  }
}

代码示例来源:origin: org.apache.camel/camel-hazelcast

@Override
public boolean confirm(String key) {
  repo.lock(key);
  try {
    return repo.replace(key, false, true);
  } finally {
    repo.unlock(key);
  }
}

代码示例来源:origin: org.apache.camel/camel-hazelcast

@Override
public boolean remove(String key) {
  repo.lock(key);
  try {
    return repo.remove(key) != null;
  } finally {
    repo.unlock(key);
  }
}

代码示例来源:origin: com.hazelcast/hazelcast-all

protected void handleMapLock(String[] args) {
  getMap().lock(args[1]);
  println("true");
}

代码示例来源:origin: com.hazelcast/hazelcast-all

protected void handleMapLock(String[] args) {
  getMap().lock(args[1]);
  println("true");
}

代码示例来源:origin: hazelcast/hazelcast-jet

protected void handleMapLock(String[] args) {
  getMap().lock(args[1]);
  println("true");
}

代码示例来源:origin: hazelcast/hazelcast-jet

protected void handleMapLock(String[] args) {
  getMap().lock(args[1]);
  println("true");
}

代码示例来源:origin: lukas-krecan/ShedLock

/**
 * Unlock the lock with its name.
 *
 * @param lockName the name of the lock to unlock.
 */
/* package */ void unlock(final String lockName) {
  log.trace("unlock - attempt : {}", lockName);
  final Instant now = Instant.now();
  final IMap<String, HazelcastLock> store = getStore();
  try {
    store.lock(lockName);
    final HazelcastLock lock = getLock(lockName);
    unlockProperly(lock, now);
  } finally {
    store.unlock(lockName);
  }
}

代码示例来源:origin: FlavioF/quartz-scheduler-hazelcast-jobstore

@Override
public void releaseAcquiredTrigger(OperableTrigger trigger) {
 TriggerKey triggerKey = trigger.getKey();
 triggersByKey.lock(triggerKey, 5, TimeUnit.SECONDS);
 try {
  storeTriggerWrapper(newTriggerWrapper(trigger, WAITING));
 } finally {
  try {
   triggersByKey.unlock(triggerKey);
  } catch (IllegalMonitorStateException ex) {
   LOG.warn("Error unlocking since it is already released.", ex);
  }
 }
}

代码示例来源:origin: FlavioF/quartz-scheduler-hazelcast-jobstore

@Override
public void pauseTrigger(TriggerKey triggerKey)
 throws JobPersistenceException {
 triggersByKey.lock(triggerKey, 5, TimeUnit.SECONDS);
 try {
  final TriggerWrapper newTrigger = newTriggerWrapper(triggersByKey.get(triggerKey), PAUSED);
  triggersByKey.set(triggerKey, newTrigger);
 } finally {
  try {
   triggersByKey.unlock(triggerKey);
  } catch (IllegalMonitorStateException ex) {
   LOG.warn("Error unlocking since it is already released.", ex);
  }
 }
}

代码示例来源:origin: io.skullabs.kikaha/kikaha-hazelcast

@Override
public Session createOrRetrieveSession( HttpServerExchange exchange ) {
  final String sessionId = retrieveSessionIdFrom( exchange );
  Session session = getSessionFromCache( sessionId );
  if ( session == null ) {
    sessionCache.lock(sessionId);
    try { session = tryToCreateAndStoreNewSession(sessionId, exchange); }
    finally { sessionCache.unlock(sessionId); }
  }
  return session;
}

代码示例来源:origin: com.hazelcast.simulator/tests-common

@TimeStep
public void timeStep(ThreadState state) {
  int key = state.randomInt(keyCount);
  long increment = state.randomInt(100);
  map.lock(key);
  try {
    Long current = map.get(key);
    map.put(key, current + increment);
    state.increments[key] += increment;
  } finally {
    map.unlock(key);
  }
}

相关文章