12

我们的很多代码都是遗留的,但我们正在转向“大数据”后端,我正在尝试宣传更新的 API 调用,鼓励使用最新的 Spring 库等。我们的问题之一是应用层 ID一代。由于我不明白的原因,更高的权威想要顺序的 BigInteger。我会通过重新生成并重新尝试失败的插入来使它们随机化,但我确实被否决了。

撇开抱怨不谈,我现在需要跨线程递增并获取 BigInteger 并以安全和高性能的方式进行操作。我以前从未使用过 AtomicReference,但它看起来非常适合这个应用程序。现在我们有一个同步的代码块,它严重地损害了我们的性能。

这是正确的方法吗?语法示例?

我应该提到这个模块的工作方式,它使用存储过程访问数据库以获取要使用的值范围。一次数以万计,因此它可能仅在 20 分钟内发生一次。这可以防止各种服务器互相踩踏,但也增加了必须将 BigInteger 设置为任意后续值的麻烦。当然,这也需要是线程安全的。

PS我仍然认为我的随机生成想法比处理所有这些线程的东西要好。BigInteger 是一个大得离谱的数字,两次生成同一个数字的几率必须接近于零。

4

2 回答 2

13

可以使用 AtomicReference 这是一个快速草稿:

public final class AtomicBigInteger {

    private final AtomicReference<BigInteger> valueHolder = new AtomicReference<>();

    public AtomicBigInteger(BigInteger bigInteger) {
        valueHolder.set(bigInteger);
    }

    public BigInteger incrementAndGet() {
        for (; ; ) {
            BigInteger current = valueHolder.get();
            BigInteger next = current.add(BigInteger.ONE);
            if (valueHolder.compareAndSet(current, next)) {
                return next;
            }
        }
    }
}

它基本上是 AtomicLong 代码的副本incrementAndGet()

于 2012-12-22T21:09:48.577 回答
6

accumulateAndGet使用 Java 8 中引入的or变得更易于管理和理解getAndAccumulate。这些允许您通过提供一个累加器函数来自动更新值,该函数将值设置为函数的结果,并且还可以返回先前的或计算的结果,具体取决于关于你需要什么。这是该类的外观示例,然后是我编写的使用它的简单示例:

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();
  }
}

一个使用它的例子:

import java.math.BigInteger;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class ABIExample {

  private static final int AVAILABLE_PROCS = Runtime.getRuntime().availableProcessors();
  private static final int INCREMENT_AMOUNT = 2_500_000;
  private static final int TASK_AMOUNT = AVAILABLE_PROCS * 2;
  private static final BigInteger EXPECTED_VALUE = BigInteger.valueOf(INCREMENT_AMOUNT)
                                                             .multiply(BigInteger
                                                                           .valueOf(TASK_AMOUNT));

  public static void main(String[] args)
      throws InterruptedException, ExecutionException {
    System.out.println("Available processors: " + AVAILABLE_PROCS);


    final ExecutorService executorService = Executors
        .newFixedThreadPool(Runtime.getRuntime().availableProcessors());

    final AtomicBigInteger atomicBigInteger = new AtomicBigInteger(BigInteger.ZERO);

    final List<Callable<Void>> incrementTasks =  IntStream.rangeClosed(1, TASK_AMOUNT)
             .mapToObj(i -> incrementTask(i, atomicBigInteger))
             .collect(Collectors.toList());
    final List<Future<Void>> futures = executorService.invokeAll(incrementTasks);
    for (Future<Void> future : futures) {
      future.get();
    }
    executorService.shutdown();
    executorService.awaitTermination(30, TimeUnit.SECONDS);
    System.out.println("Final value: " + atomicBigInteger.get());
    final boolean areEqual = EXPECTED_VALUE.equals(atomicBigInteger.get());
    System.out.println("Does final value equal expected? - " + areEqual);
  }

  private static Callable<Void> incrementTask(
      final int taskNumber,
      final AtomicBigInteger atomicBigInteger
  ) {
    return () -> {
      for (int increment = 0; increment < INCREMENT_AMOUNT; increment++) {
        atomicBigInteger.incrementAndGet();
      }
      System.out.println("Task #" + taskNumber + " Completed");
      return null;
    };

  }
}

在我的机器上运行示例的输出:

Available processors: 8
Task #3 Completed
Task #8 Completed
Task #7 Completed
Task #6 Completed
Task #5 Completed
Task #2 Completed
Task #4 Completed
Task #1 Completed
Task #9 Completed
Task #10 Completed
Task #11 Completed
Task #13 Completed
Task #16 Completed
Task #12 Completed
Task #14 Completed
Task #15 Completed
Final value: 80000000
Does final value equal expected? - true
于 2016-04-09T16:54:12.177 回答