Java Code Examples for java.util.concurrent.ConcurrentMap#compute()

The following examples show how to use java.util.concurrent.ConcurrentMap#compute() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: incubator-nemo   File: ContextManager.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Initiates a context and stores to the specified map.
 *
 * @param contexts             map for storing context
 * @param transferIndexCounter counter for generating transfer index
 * @param dataDirection        data direction to include in the context id
 * @param contextGenerator     a function that returns context from context id
 * @param executorId           id of the remote executor
 * @param <T>                  {@link ByteInputContext} or {@link ByteOutputContext}
 * @param isPipe               is a pipe context
 * @return generated context
 */
<T extends ByteTransferContext> T newContext(final ConcurrentMap<Integer, T> contexts,
                                             final AtomicInteger transferIndexCounter,
                                             final ByteTransferDataDirection dataDirection,
                                             final Function<ContextId, T> contextGenerator,
                                             final String executorId,
                                             final boolean isPipe) {
  setRemoteExecutorId(executorId);
  final int transferIndex = transferIndexCounter.getAndIncrement();
  final ContextId contextId = new ContextId(localExecutorId, executorId, dataDirection, transferIndex, isPipe);
  final T context = contexts.compute(transferIndex, (index, existingContext) -> {
    if (existingContext != null) {
      throw new RuntimeException(String.format("Duplicate ContextId: %s", contextId));
    }
    return contextGenerator.apply(contextId);
  });
  channel.writeAndFlush(context).addListener(context.getChannelWriteListener());
  return context;
}
 
Example 2
Source Project: nemo   File: ContextManager.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Initiates a context and stores to the specified map.
 * @param contexts map for storing context
 * @param transferIndexCounter counter for generating transfer index
 * @param dataDirection data direction to include in the context id
 * @param contextGenerator a function that returns context from context id
 * @param executorId id of the remote executor
 * @param <T> {@link ByteInputContext} or {@link ByteOutputContext}
 * @return generated context
 */
<T extends ByteTransferContext> T newContext(final ConcurrentMap<Integer, T> contexts,
                                             final AtomicInteger transferIndexCounter,
                                             final ByteTransferDataDirection dataDirection,
                                             final Function<ContextId, T> contextGenerator,
                                             final String executorId) {
  setRemoteExecutorId(executorId);
  final int transferIndex = transferIndexCounter.getAndIncrement();
  final ContextId contextId = new ContextId(localExecutorId, executorId, dataDirection, transferIndex);
  final T context = contexts.compute(transferIndex, (index, existingContext) -> {
    if (existingContext != null) {
      throw new RuntimeException(String.format("Duplicate ContextId: %s", contextId));
    }
    return contextGenerator.apply(contextId);
  });
  channel.writeAndFlush(context).addListener(context.getChannelWriteListener());
  return context;
}
 
Example 3
Source Project: caffeine   File: ConcurrentHashMap8Test.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * compute does not replace if the function returns null
 */
public void testCompute() {
    ConcurrentMap map = map5();
    map.compute(six, (x, y) -> null);
    assertFalse(map.containsKey(six));
}
 
Example 4
Source Project: caffeine   File: ConcurrentHashMap8Test.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * compute removes when the given key is present and function returns null
 */
public void testCompute4() {
    ConcurrentMap map = map5();
    map.compute(one, (x, y) -> null);
    assertFalse(map.containsKey(one));
}