gpt4 book ai didi

java - 如何测试 AtomicBoolean 的原子性?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:06:15 26 4
gpt4 key购买 nike

我正在为 AtomicInteger 和 AtomicBoolean 编写单元测试。它们将用作引用测试,用于测试 objective-c 中这些类的仿真,用于翻译项目。

我认为 AtomicInteger 测试效果很好,基本上是通过在大量 for 循环中执行可预测数量的递增、递减、加减操作,每个循环都在自己的线程中运行(每个操作类型有许多线程)。实际操作使用 CountDownLatch 同时开始。

当所有线程都完成后,我根据线程数、每个线程的迭代次数和每次迭代的预期增加/减少,通过将原子整数与预期整数值进行比较来断言。该测试通过。

但是如何测试 AtomicBoolean? 基本操作是 get 和 set,因此在多个线程中多次调用它并期望最终结果为 true 或 false 似乎没有意义。我想的方向是使用两个应该始终具有相反值的 AtomicBooleans。像这样:

@Test
public void testAtomicity() throws Exception {

// ==== SETUP ====
final AtomicBoolean booleanA = new AtomicBoolean(true);
final AtomicBoolean booleanB = new AtomicBoolean(false);

final int threadCount = 50;

final int iterationsPerThread = 5000;

final CountDownLatch startSignalLatch = new CountDownLatch(1);
final CountDownLatch threadsFinishedLatch = new CountDownLatch(threadCount);

final AtomicBoolean assertFailed = new AtomicBoolean(false);

// ==== EXECUTE: start all threads ====
for (int i = 0; i < threadCount; i++) {

// ==== Create the thread =====
AtomicOperationsThread thread;
thread = new AtomicOperationsThread("Thread #" + i, booleanA, booleanB, startSignalLatch, threadsFinishedLatch, iterationsPerThread, assertFailed);
System.out.println("Creating Thread #" + i);

// ==== Start the thread (each thread will wait until the startSignalLatch is triggered) =====
thread.start();
}

startSignalLatch.countDown();

// ==== VERIFY: that the AtomicInteger has the expected value after all threads have finished ====
final boolean allThreadsFinished;
allThreadsFinished = threadsFinishedLatch.await(60, TimeUnit.SECONDS);

assertTrue("Not all threads have finished before reaching the timeout", allThreadsFinished);
assertFalse(assertFailed.get());

}

private static class AtomicOperationsThread extends Thread {

// ##### Instance variables #####

private final CountDownLatch startSignalLatch;
private final CountDownLatch threadsFinishedLatch;

private final int iterations;

private final AtomicBoolean booleanA, booleanB;

private final AtomicBoolean assertFailed;

// ##### Constructor #####

private AtomicOperationsThread(final String name, final AtomicBoolean booleanA, final AtomicBoolean booleanB, final CountDownLatch startSignalLatch, final CountDownLatch threadsFinishedLatch, final int iterations, final AtomicBoolean assertFailed) {

super(name);
this.booleanA = booleanA;
this.booleanB = booleanB;
this.startSignalLatch = startSignalLatch;
this.threadsFinishedLatch = threadsFinishedLatch;
this.iterations = iterations;
this.assertFailed = assertFailed;
}

// ##### Thread implementation #####

@Override
public void run() {

super.run();

// ==== Wait for the signal to start (so all threads are executed simultaneously) =====
try {
System.out.println(this.getName() + " has started. Awaiting startSignal.");
startSignalLatch.await(); /* Awaiting start signal */
} catch (InterruptedException e) {
throw new RuntimeException("The startSignalLatch got interrupted.", e);
}

// ==== Perform the atomic operations =====
for (int i = 0; i < iterations; i++) {

final boolean booleanAChanged;
booleanAChanged = booleanA.compareAndSet(!booleanB.get(), booleanB.getAndSet(booleanA.get())); /* Set A to the current value of B if A is currently the opposite of B, then set B to the current value of A */

if (!booleanAChanged){
assertFailed.set(true);
System.out.println("Assert failed in thread: " + this.getName());
}
}

// ==== Mark this thread as finished =====
threadsFinishedLatch.countDown();
}
}

这对一个线程有效,但对多个线程无效。我猜这是因为 booleanAChanged = booleanA.compareAndSet(!booleanB.get(), booleanB.getAndSet(booleanA.get())); 不是一个原子操作。

有什么建议吗?

最佳答案

我会专注于 compareAndSet,这是 AtomicBoolean 和普通 boolean 之间的真正区别。

例如,使用compareAndSet(false, true) 来控制临界区。循环执行直到返回false,然后进入临界区。在临界区,做一些如果有两个或多个线程同时运行就极有可能失败的事情。例如,在读取旧值和写入新值之间通过短暂的 hibernate 来增加计数器。在临界区的末尾,将 AtomicBoolean 设置为 false。

在启动线程之前将 AtomicBoolean 初始化为 false,将 globalCounter 初始化为零。

for(int i=0; i<iterations; i++) {
while (!AtomicBooleanTest.atomic.compareAndSet(false, true));
int oldValue = AtomicBooleanTest.globalCounter;
Thread.sleep(1);
AtomicBooleanTest.globalCounter = oldValue + 1;
AtomicBooleanTest.atomic.set(false);
}

最后,globalCounter 值应为 t*iterations,其中 t 是线程数。

线程数应该与硬件可以同时运行的数量相似——这在多处理器上比在单处理器上更容易失败。最高的失败风险发生在 AtomicBoolean 变为假之后。所有可用的处理器都应该同时尝试获得对它的独占访问权,将其视为 false,然后自动将其更改为 true。

关于java - 如何测试 AtomicBoolean 的原子性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17414924/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com