java.util.concurrent.ConcurrentHashMap.computeIfPresent()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(8.5k)|赞(0)|评价(0)|浏览(109)

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

ConcurrentHashMap.computeIfPresent介绍

暂无

代码示例

代码示例来源:origin: apache/hbase

/**
 * Decrements the ref count of k, and removes from map if ref count == 0.
 * @param k the key to remove
 * @return the value associated with the specified key or null if key is removed from map.
 */
V remove(K k) {
 Payload<V> p = map.computeIfPresent(k, (k1, v) -> --v.refCount <= 0 ? null : v);
 return p == null ? null : p.v;
}

代码示例来源:origin: apache/hbase

private void decrementUgiReference(UserGroupInformation ugi) {
 ugiReferenceCounter.computeIfPresent(ugi,
   new BiFunction<UserGroupInformation, Integer, Integer>() {
    @Override
    public Integer apply(UserGroupInformation key, Integer value) {
     return value > 1 ? --value : null;
    }
  });
}

代码示例来源:origin: wildfly/wildfly

public V computeIfPresent(final K key, final BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
  return backingMap.computeIfPresent(key, remappingFunction);
}

代码示例来源:origin: apache/ignite

/**
 * @param c Cache context closure.
 */
void forAllCaches(final IgniteInClosure<GridCacheContext> c) {
  for (Integer cacheId : ctxMap.keySet()) {
    ctxMap.computeIfPresent(cacheId,
      (cacheId1, ctx) -> {
        c.apply(ctx);
        return ctx;
      }
    );
  }
}

代码示例来源:origin: ben-manes/caffeine

@Override
public boolean replace(K key, V oldValue, V newValue) {
 requireNonNull(oldValue);
 requireNonNull(newValue);
 @SuppressWarnings({"unchecked", "rawtypes"})
 V[] prev = (V[]) new Object[1];
 data.computeIfPresent(key, (k, v) -> {
  if (v.equals(oldValue)) {
   if (newValue != v) {
    writer.write(key, newValue);
   }
   prev[0] = v;
   return newValue;
  }
  return v;
 });
 boolean replaced = (prev[0] != null);
 if (hasRemovalListener() && replaced && (prev[0] != newValue)) {
  notifyRemoval(key, prev[0], RemovalCause.REPLACED);
 }
 return replaced;
}

代码示例来源:origin: ben-manes/caffeine

@Override
public boolean remove(Object key, Object value) {
 if (value == null) {
  requireNonNull(key);
  return false;
 }
 @SuppressWarnings("unchecked")
 K castKey = (K) key;
 @SuppressWarnings({"unchecked", "rawtypes"})
 V[] oldValue = (V[]) new Object[1];
 data.computeIfPresent(castKey, (k, v) -> {
  if (v.equals(value)) {
   writer.delete(castKey, v, RemovalCause.EXPLICIT);
   oldValue[0] = v;
   return null;
  }
  return v;
 });
 boolean removed = (oldValue[0] != null);
 if (hasRemovalListener() && removed) {
  notifyRemoval(castKey, oldValue[0], RemovalCause.EXPLICIT);
 }
 return removed;
}

代码示例来源:origin: ben-manes/caffeine

@Override
public @Nullable V computeIfPresent(K key,
  BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
 requireNonNull(remappingFunction);
 // optimistic fast path due to computeIfAbsent always locking
 if (!data.containsKey(key)) {
  return null;
 }
 // ensures that the removal notification is processed after the removal has completed
 @SuppressWarnings({"unchecked", "rawtypes"})
 V[] oldValue = (V[]) new Object[1];
 RemovalCause[] cause = new RemovalCause[1];
 V nv = data.computeIfPresent(key, (K k, V value) -> {
  BiFunction<? super K, ? super V, ? extends V> function =
    statsAware(remappingFunction, /* recordMiss */ false, /* recordLoad */ true);
  V newValue = function.apply(k, value);
  cause[0] = (newValue == null) ? RemovalCause.EXPLICIT : RemovalCause.REPLACED;
  if (hasRemovalListener() && (newValue != value)) {
   oldValue[0] = value;
  }
  return newValue;
 });
 if (oldValue[0] != null) {
  notifyRemoval(key, oldValue[0], cause[0]);
 }
 return nv;
}

代码示例来源:origin: ben-manes/caffeine

@Override
public @Nullable V replace(K key, V value) {
 requireNonNull(value);
 @SuppressWarnings({"unchecked", "rawtypes"})
 V[] oldValue = (V[]) new Object[1];
 data.computeIfPresent(key, (k, v) -> {
  if (value != v) {
   writer.write(key, value);
  }
  oldValue[0] = v;
  return value;
 });
 if (hasRemovalListener() && (oldValue[0] != null) && (oldValue[0] != value)) {
  notifyRemoval(key, value, RemovalCause.REPLACED);
 }
 return oldValue[0];
}

代码示例来源:origin: ben-manes/caffeine

@Override
public @Nullable V remove(Object key) {
 @SuppressWarnings("unchecked")
 K castKey = (K) key;
 @SuppressWarnings({"unchecked", "rawtypes"})
 V[] oldValue = (V[]) new Object[1];
 if (writer == CacheWriter.disabledWriter()) {
  oldValue[0] = data.remove(key);
 } else {
  data.computeIfPresent(castKey, (k, v) -> {
   writer.delete(castKey, v, RemovalCause.EXPLICIT);
   oldValue[0] = v;
   return null;
  });
 }
 if (hasRemovalListener() && (oldValue[0] != null)) {
  notifyRemoval(castKey, oldValue[0], RemovalCause.EXPLICIT);
 }
 return oldValue[0];
}

代码示例来源:origin: apache/incubator-druid

@Override
public boolean removeSegment(String dataSourceName, final String segmentId)
{
 try {
  final boolean removed = removeSegmentFromTable(segmentId);
  // Call iteratePossibleParsingsWithDataSource() outside of dataSources.computeIfPresent() because the former is a
  // potentially expensive operation, while lambda to be passed into computeIfPresent() should preferably run fast.
  List<SegmentId> possibleSegmentIds = SegmentId.iteratePossibleParsingsWithDataSource(dataSourceName, segmentId);
  dataSources.computeIfPresent(
    dataSourceName,
    (dsName, dataSource) -> {
     for (SegmentId possibleSegmentId : possibleSegmentIds) {
      if (dataSource.removeSegment(possibleSegmentId) != null) {
       break;
      }
     }
     // Returning null from the lambda here makes the ConcurrentHashMap to remove the current entry.
     //noinspection ReturnOfNull
     return dataSource.isEmpty() ? null : dataSource;
    }
  );
  return removed;
 }
 catch (Exception e) {
  log.error(e, e.toString());
  return false;
 }
}

代码示例来源:origin: apache/incubator-druid

@Override
public boolean removeSegment(SegmentId segmentId)
{
 try {
  final boolean removed = removeSegmentFromTable(segmentId.toString());
  dataSources.computeIfPresent(
    segmentId.getDataSource(),
    (dsName, dataSource) -> {
     dataSource.removeSegment(segmentId);
     // Returning null from the lambda here makes the ConcurrentHashMap to remove the current entry.
     //noinspection ReturnOfNull
     return dataSource.isEmpty() ? null : dataSource;
    }
  );
  return removed;
 }
 catch (Exception e) {
  log.error(e, e.toString());
  return false;
 }
}

代码示例来源:origin: ben-manes/caffeine

RemovalCause[] cause = new RemovalCause[1];
data.computeIfPresent(nodeFactory.newLookupKey(key), (kR, node) -> {
 synchronized (node) {
  oldKey[0] = node.getKey();

代码示例来源:origin: ben-manes/caffeine

int[] oldWeight = new int[1];
long[] now = new long[1];
Node<K, V> node = data.computeIfPresent(nodeFactory.newLookupKey(key), (k, n) -> {
 synchronized (n) {
  nodeKey[0] = n.getKey();

代码示例来源:origin: ben-manes/caffeine

RemovalCause[] cause = new RemovalCause[1];
data.computeIfPresent(nodeFactory.newLookupKey(key), (k, n) -> {
 synchronized (n) {
  oldValue[0] = n.getValue();

代码示例来源:origin: ben-manes/caffeine

long[] now = new long[1];
int weight = weigher.weigh(key, value);
Node<K, V> node = data.computeIfPresent(nodeFactory.newLookupKey(key), (k, n) -> {
 synchronized (n) {
  nodeKey[0] = n.getKey();

代码示例来源:origin: ben-manes/caffeine

RemovalCause[] cause = new RemovalCause[1];
data.computeIfPresent(node.getKeyReference(), (k, n) -> {
 if (n != node) {
  return n;

代码示例来源:origin: com.github.ben-manes.caffeine/caffeine

@Override
public @Nullable V replace(K key, V value) {
 requireNonNull(value);
 @SuppressWarnings({"unchecked", "rawtypes"})
 V[] oldValue = (V[]) new Object[1];
 data.computeIfPresent(key, (k, v) -> {
  if (value != v) {
   writer.write(key, value);
  }
  oldValue[0] = v;
  return value;
 });
 if (hasRemovalListener() && (oldValue[0] != null) && (oldValue[0] != value)) {
  notifyRemoval(key, value, RemovalCause.REPLACED);
 }
 return oldValue[0];
}

代码示例来源:origin: com.github.ben-manes.caffeine/caffeine

@Override
public boolean replace(K key, V oldValue, V newValue) {
 requireNonNull(oldValue);
 requireNonNull(newValue);
 @SuppressWarnings({"unchecked", "rawtypes"})
 V[] prev = (V[]) new Object[1];
 data.computeIfPresent(key, (k, v) -> {
  if (v.equals(oldValue)) {
   if (newValue != v) {
    writer.write(key, newValue);
   }
   prev[0] = v;
   return newValue;
  }
  return v;
 });
 boolean replaced = (prev[0] != null);
 if (hasRemovalListener() && replaced && (prev[0] != newValue)) {
  notifyRemoval(key, prev[0], RemovalCause.REPLACED);
 }
 return replaced;
}

代码示例来源:origin: com.github.ben-manes.caffeine/caffeine

@Override
public @Nullable V remove(Object key) {
 @SuppressWarnings("unchecked")
 K castKey = (K) key;
 @SuppressWarnings({"unchecked", "rawtypes"})
 V[] oldValue = (V[]) new Object[1];
 if (writer == CacheWriter.disabledWriter()) {
  oldValue[0] = data.remove(key);
 } else {
  data.computeIfPresent(castKey, (k, v) -> {
   writer.delete(castKey, v, RemovalCause.EXPLICIT);
   oldValue[0] = v;
   return null;
  });
 }
 if (hasRemovalListener() && (oldValue[0] != null)) {
  notifyRemoval(castKey, oldValue[0], RemovalCause.EXPLICIT);
 }
 return oldValue[0];
}

代码示例来源:origin: ben-manes/caffeine

RemovalCause[] actualCause = new RemovalCause[1];
data.computeIfPresent(node.getKeyReference(), (k, n) -> {
 if (n != node) {
  return n;

相关文章

微信公众号

最新文章

更多