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

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

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

AtomicInteger.getAndAccumulate介绍

[英]Atomically updates the current value with the results of applying the given function to the current and given values, returning the previous value. The function should be side-effect-free, since it may be re-applied when attempted updates fail due to contention among threads. The function is applied with the current value as its first argument, and the given update as the second argument.
[中]将给定函数应用于当前值和给定值的结果以原子方式更新当前值,并返回以前的值。该函数应该没有副作用,因为当尝试的更新由于线程之间的争用而失败时,可以重新应用该函数。应用函数时,当前值作为第一个参数,给定的更新作为第二个参数。

代码示例

代码示例来源:origin: micronaut-projects/micronaut-core

/**
   * @param serviceInstances A list of service instances
   * @return The next available instance or a {@link NoAvailableServiceException} if none
   */
  protected ServiceInstance getNextAvailable(List<ServiceInstance> serviceInstances) {
    List<ServiceInstance> availableServices = serviceInstances.stream()
      .filter(si -> si.getHealthStatus().equals(HealthStatus.UP))
      .collect(Collectors.toList());
    int len = availableServices.size();
    if (len == 0) {
      throw new NoAvailableServiceException(getServiceID());
    }
    int i = index.getAndAccumulate(len, (cur, n) -> cur >= n - 1 ? 0 : cur + 1);
    try {
      return availableServices.get(i);
    } catch (IndexOutOfBoundsException e) {
      throw new NoAvailableServiceException(getServiceID());
    }
  }
}

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

/**
 * Updates the quorum size if it's larger than the current value. Ignores, if it's not.
 */
void setLargerQuorumSize(int newQuorumSize) {
  quorumSize.getAndAccumulate(newQuorumSize, Math::max);
}

代码示例来源:origin: org.xipki/ca-server

public long nextId() {
 long now = System.currentTimeMillis();
 long ret = now - epoch;
 ret <<= 10;
 ret += offset.getAndAccumulate(MAX_OFFSET, accumulatorFunction);
 ret <<= 7;
 ret += shardId;
 return ret;
}

代码示例来源:origin: Syncleus/aparapi

@OpenCLMapping(atomic32 = true, mapTo = "atomic_xor")
protected final int atomicXor(AtomicInteger p, int val) {
  return p.getAndAccumulate(val, xorOperator);
}

代码示例来源:origin: Syncleus/aparapi

@OpenCLMapping(atomic32 = true, mapTo = "atomic_and")
protected final int atomicAnd(AtomicInteger p, int val) {
  return p.getAndAccumulate(val, andOperator);
}

代码示例来源:origin: Syncleus/aparapi

@OpenCLMapping(atomic32 = true, mapTo = "atomic_or")
protected final int atomicOr(AtomicInteger p, int val) {
  return p.getAndAccumulate(val, orOperator);
}

代码示例来源:origin: Syncleus/aparapi

@OpenCLMapping(atomic32 = true, mapTo = "atomic_min")
protected final int atomicMin(AtomicInteger p, int val) {
  return p.getAndAccumulate(val, minOperator);
}

代码示例来源:origin: Syncleus/aparapi

@OpenCLMapping(atomic32 = true, mapTo = "atomic_max")
protected final int atomicMax(AtomicInteger p, int val) {
  return p.getAndAccumulate(val, maxOperator);
}

代码示例来源:origin: io.micronaut/http-client

/**
   * @param serviceInstances A list of service instances
   * @return The next available instance or a {@link NoAvailableServiceException} if none
   */
  protected ServiceInstance getNextAvailable(List<ServiceInstance> serviceInstances) {
    List<ServiceInstance> availableServices = serviceInstances.stream()
      .filter(si -> si.getHealthStatus().equals(HealthStatus.UP))
      .collect(Collectors.toList());
    int len = availableServices.size();
    if (len == 0) {
      throw new NoAvailableServiceException(getServiceID());
    }
    int i = index.getAndAccumulate(len, (cur, n) -> cur >= n - 1 ? 0 : cur + 1);
    return availableServices.get(i);
  }
}

代码示例来源:origin: dubreuia/intellij-plugin-save-actions

private boolean handleUsage(@NotNull PsiMember member,
              @Nullable PsiClass memberClass,
              @NotNull PsiFile memberFile,
              @NotNull AtomicInteger maxLevel,
              @Nullable PsiPackage memberPackage,
              @NotNull PsiElement element,
              @NotNull PsiFile psiFile,
              @NotNull AtomicBoolean foundUsage) {
 foundUsage.set(true);
 if (!(psiFile instanceof PsiJavaFile)) {
  log("     refd from " + psiFile.getName() + "; set to public");
  maxLevel.set(PsiUtil.ACCESS_LEVEL_PUBLIC);
  return false; // referenced from XML, has to be public
 }
 @PsiUtil.AccessLevel
 int level = getEffectiveLevel(element, psiFile, member, memberFile, memberClass, memberPackage);
 log("    ref in file " + psiFile.getName() + "; level = " + PsiUtil.getAccessModifier(level) + "; (" + element + ")");
 maxLevel.getAndAccumulate(level, Math::max);
 return level != PsiUtil.ACCESS_LEVEL_PUBLIC;
}

代码示例来源:origin: io.micronaut/micronaut-http-client

/**
   * @param serviceInstances A list of service instances
   * @return The next available instance or a {@link NoAvailableServiceException} if none
   */
  protected ServiceInstance getNextAvailable(List<ServiceInstance> serviceInstances) {
    List<ServiceInstance> availableServices = serviceInstances.stream()
      .filter(si -> si.getHealthStatus().equals(HealthStatus.UP))
      .collect(Collectors.toList());
    int len = availableServices.size();
    if (len == 0) {
      throw new NoAvailableServiceException(getServiceID());
    }
    int i = index.getAndAccumulate(len, (cur, n) -> cur >= n - 1 ? 0 : cur + 1);
    try {
      return availableServices.get(i);
    } catch (IndexOutOfBoundsException e) {
      throw new NoAvailableServiceException(getServiceID());
    }
  }
}

相关文章