java.util.concurrent.atomic.AtomicReference.accumulateAndGet()方法的使用及代码示例

x33g5p2x  于2022-01-15 转载在 其他  
字(8.0k)|赞(0)|评价(0)|浏览(106)

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

AtomicReference.accumulateAndGet介绍

暂无

代码示例

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

public Object increment(String key, Object obj, BinaryOperator mergeFunction)
{
 return objectCounters.computeIfAbsent(key, k -> new AtomicReference()).accumulateAndGet(obj, mergeFunction);
}

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

private String getCurrentKnownLeader(final boolean cached) throws IOException
{
 final String leader = currentKnownLeader.accumulateAndGet(
   null,
   (current, given) -> current == null || !cached ? pickOneHost() : current
 );
 if (leader == null) {
  throw new IOE("No known server");
 } else {
  return leader;
 }
}

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

@SafeVarargs
public static <T> CompletableFuture<T> allOf(
  T defaultValue,
  BinaryOperator<T> merger, 
  CompletableFuture<T>... futures) {
  
  @SuppressWarnings("unchecked")
  final CompletableFuture<Void>[] accumulators 
    = (CompletableFuture<Void>[]) Array.newInstance(
      CompletableFuture.class, 
      futures.length
    );
  final AtomicReference<T> result = new AtomicReference<>(defaultValue);
  for (int i = 0; i < futures.length; i++) {
    final CompletableFuture<T> future = futures[i];
    accumulators[i] = future.thenAcceptAsync(r -> result.accumulateAndGet(r, merger));
  }
  
  return CompletableFuture.allOf(accumulators)
    .thenApplyAsync(v -> result.get());
}

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

error.accumulateAndGet( e, Exceptions::chain );

代码示例来源:origin: palantir/atlasdb

@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
private void accumulateCachedValue(Optional<ValueAndBound<T>> valueAndBound) {
  valueAndBound.ifPresent(presentValue ->
      cache.accumulateAndGet(presentValue, this::chooseValueWithGreaterBound)
  );
}

代码示例来源:origin: palantir/atlasdb

@Override
  protected T loadValue() {
    try {
      return previousValue.accumulateAndGet(supplier.get(), reducer);
    } catch (Exception e) {
      T existingValue = previousValue.get();
      logger.info("An exception occurred when trying to update the {} gauge for tracking purposes."
              + " Returning the last known value of {}.",
          SafeArg.of("gaugeName", shortName),
          SafeArg.of("existingValue", existingValue),
          e);
      return existingValue;
    }
  }
};

代码示例来源:origin: org.elasticsearch/elasticsearch

@Override
  public void onFailure(Exception e) {
    RemoteTransportException exception =
        new RemoteTransportException("error while communicating with remote cluster [" + clusterName + "]", e);
    if (transportException.compareAndSet(null, exception) == false) {
      exception = transportException.accumulateAndGet(exception, (previous, current) -> {
        current.addSuppressed(previous);
        return current;
      });
    }
    if (responsesCountDown.countDown()) {
      listener.onFailure(exception);
    }
  }
});

代码示例来源:origin: stackoverflow.com

public static String updateLongestString(AtomicReference<String> observed, String x) {

  final String longestString = observed.accumulateAndGet(x, maxBy((str1, str2) -> {
    LOGGER.info("Received str1: {}, str2: {}", str1, str2);
    return str1.length() - str2.length();
  }));

  LOGGER.info("New observed: {}.", longestString);

  return longestString;
}

代码示例来源:origin: com.palantir.atlasdb/atlasdb-coordination-impl

@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
private void accumulateCachedValue(Optional<ValueAndBound<T>> valueAndBound) {
  valueAndBound.ifPresent(presentValue ->
      cache.accumulateAndGet(presentValue, this::chooseValueWithGreaterBound)
  );
}

代码示例来源:origin: org.apache.druid/druid-indexing-service

public Object increment(String key, Object obj, BinaryOperator mergeFunction)
{
 return objectCounters.computeIfAbsent(key, k -> new AtomicReference()).accumulateAndGet(obj, mergeFunction);
}

代码示例来源:origin: stackoverflow.com

public static void main(String[] args) 
 {             
    AtomicReference<String> longest = new AtomicReference<>();
    List<String> words = Arrays.asList("him", "he", "thanks", "strings", "congratulations", "platform");
    words.parallelStream().forEach(next -> {                
      longest.accumulateAndGet(next, (a,b) ->
          a != null && a.length() > b.length() ? a : b 
         );                  
      });
    System.out.println(longest.get());
 }

代码示例来源:origin: one.util/streamex

@Override
  public void accept(T next) {
    if(accRef == null) {
      acc = op.apply(acc, next);
    } else {
      acc = accRef.accumulateAndGet(next, op);
    }
  }
}

代码示例来源:origin: stackoverflow.com

class Data
{
  long v1, v2;

  Data add(Data another){ ... }
}

AtomicReference<Data> aData = ...;

public void outTick(long elapsedMsgTime)
{
  Data delta = new Data(1, elapsedMsgTime);

  aData.accumulateAndGet( delta, Data:add );
}

代码示例来源:origin: stackoverflow.com

import java.math.BigInteger;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicReference;

public final class AtomicBigInteger {

 private final AtomicReference<BigInteger> bigInteger;

 public AtomicBigInteger(final BigInteger bigInteger) {
  this.bigInteger = new AtomicReference<>(Objects.requireNonNull(bigInteger));
 }

 // Method references left out for demonstration purposes
 public BigInteger incrementAndGet() {
  return bigInteger.accumulateAndGet(BigInteger.ONE, (previous, x) -> previous.add(x));
 }

 public BigInteger getAndIncrement() {
  return bigInteger.getAndAccumulate(BigInteger.ONE, (previous, x) -> previous.add(x));
 }

 public BigInteger get() {
  return bigInteger.get();
 }
}

代码示例来源:origin: com.palantir.atlasdb/atlasdb-impl-shared

@Override
  protected T loadValue() {
    try {
      return previousValue.accumulateAndGet(supplier.get(), reducer);
    } catch (Exception e) {
      T existingValue = previousValue.get();
      logger.info("An exception occurred when trying to update the {} gauge for tracking purposes."
              + " Returning the last known value of {}.",
          SafeArg.of("gaugeName", shortName),
          SafeArg.of("existingValue", existingValue),
          e);
      return existingValue;
    }
  }
};

代码示例来源:origin: io.druid/druid-server

private String getCurrentKnownLeader(final boolean cached) throws IOException
{
 final String leader = currentKnownLeader.accumulateAndGet(
   null,
   (current, given) -> current == null || !cached ? pickOneHost() : current
 );
 if (leader == null) {
  throw new IOE("No known server");
 } else {
  return leader;
 }
}

代码示例来源:origin: org.apache.druid/druid-server

private String getCurrentKnownLeader(final boolean cached) throws IOException
{
 final String leader = currentKnownLeader.accumulateAndGet(
   null,
   (current, given) -> current == null || !cached ? pickOneHost() : current
 );
 if (leader == null) {
  throw new IOE("No known server");
 } else {
  return leader;
 }
}

代码示例来源:origin: org.apache.servicemix.bundles/org.apache.servicemix.bundles.elasticsearch

@Override
  public void onFailure(Exception e) {
    RemoteTransportException exception =
        new RemoteTransportException("error while communicating with remote cluster [" + clusterName + "]", e);
    if (transportException.compareAndSet(null, exception) == false) {
      exception = transportException.accumulateAndGet(exception, (previous, current) -> {
        current.addSuppressed(previous);
        return current;
      });
    }
    if (responsesCountDown.countDown()) {
      listener.onFailure(exception);
    }
  }
});

代码示例来源:origin: com.strapdata.elasticsearch/elasticsearch

@Override
  public void onFailure(Exception e) {
    TransportException exception = new TransportException("unable to communicate with remote cluster [" +
      clusterName + "]", e);
    if (transportException.compareAndSet(null, exception) == false) {
      exception = transportException.accumulateAndGet(exception, (previous, current) -> {
        current.addSuppressed(previous);
        return current;
      });
    }
    if (responsesCountDown.countDown()) {
      listener.onFailure(exception);
    }
  }
});

代码示例来源:origin: org.osgi/osgi.cmpn

@Override
public Promise<T> reduce(T identity, BinaryOperator<T> accumulator) {
  Deferred<T> d = promiseFactory.deferred();
  AtomicReference<T> iden = new AtomicReference<T>(identity);
  updateNext(event -> {
    try {
      switch(event.getType()) {
        case DATA:
          iden.accumulateAndGet(event.getData(), accumulator);
          return CONTINUE;
        case CLOSE:
          d.resolve(iden.get());
          break;
        case ERROR:
          d.fail(event.getFailure());
          break;
      }
      close(event.nodata());
      return ABORT;
    } catch (Exception e) {
      close(PushEvent.error(e));
      return ABORT;
    }
  });
  begin();
  return d.getPromise();
}

相关文章