Java 如何测试原子性布尔原子性?
我正在为AtomicInteger和AtomicBoolean编写单元测试。它们将被用作在objective-c中测试这些类的模拟的参考测试,用于翻译项目 我认为AtomicInteger测试运行得很好,基本上是通过在大量for循环中执行可预测数量的递增、递减、加法和减法操作,每个循环在各自的线程中运行(每个操作类型有许多线程)。实际操作使用倒计时锁存器同时开始 当所有线程都完成时,我通过比较原子整数和基于线程数、每个线程的迭代次数以及每次迭代的预期增加/减少的预期整数值来断言。这个测试通过了 但是如何测试AtomicBoolean呢?基本操作是get和set,因此在多个线程中多次调用并期望最终结果为true或false似乎没有意义。我想的方向是使用两个原子布尔,它们的值应该总是相反的。像这样:Java 如何测试原子性布尔原子性?,java,multithreading,unit-testing,concurrency,Java,Multithreading,Unit Testing,Concurrency,我正在为AtomicInteger和AtomicBoolean编写单元测试。它们将被用作在objective-c中测试这些类的模拟的参考测试,用于翻译项目 我认为AtomicInteger测试运行得很好,基本上是通过在大量for循环中执行可预测数量的递增、递减、加法和减法操作,每个循环在各自的线程中运行(每个操作类型有许多线程)。实际操作使用倒计时锁存器同时开始 当所有线程都完成时,我通过比较原子整数和基于线程数、每个线程的迭代次数以及每次迭代的预期增加/减少的预期整数值来断言。这个测试通过了
@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();
}
}
@测试
public void testAtomicity()引发异常{
//==设置====
最终原子布尔布尔A=新原子布尔(真);
最终原子布尔布尔B=新原子布尔(假);
最终int螺纹数=50;
最终int迭代perthread=5000;
最终倒计时闩锁启动信号拉奇=新倒计时闩锁(1);
最终倒计时闩锁螺纹FinishedLatch=新倒计时闩锁(螺纹计数);
最终AtomicBoolean assertFailed=新的AtomicBoolean(false);
//==执行:启动所有线程====
对于(int i=0;i
这可以在一个线程中工作,但在多个线程中失败。我猜这是因为booleanAChanged=booleanA.compareAndSet(!booleanB.get(),booleanB.getAndSet(booleanA.get())代码>不是一个原子操作
有什么建议吗?这是四个原子操作。如果你只想让一个布尔值成为另一个布尔值的倒数,只要有一个布尔值并不断切换即可。您可以从该值计算另一个。我将集中讨论比较数据集
,这是原子布尔
和普通布尔
之间的真正区别
例如,使用compareAndSet(false,true)
控制关键区域。循环执行,直到返回false,然后输入临界区域。在关键区域,如果两个或多个线程同时运行,则执行一些很可能失败的操作。例如,在读取旧值和写入新值之间的短睡眠时间内递增计数器。在关键区域的末尾,将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);
}
for(int i=0;i正如您所指出的,我认为测试这一点要比测试一个AtomicInteger
困难。可能值的空间要小得多,因此可能出错的事情的空间也要小得多