我正在为AtomicInteger和AtomicBoolean编写单元测试。它们将被用作在objective中测试这些类的仿真的参考测试,用于翻译的项目。
我认为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()));不是一个原子操作。
有什么建议吗?
发布于 2013-07-01 23:50:15
我会集中讨论compareAndSet,这是AtomicBoolean和普通boolean的真正区别。
例如,使用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);
}最后,globalCounter值应该是t*iterations,其中t是线程数。
线程的数量应该与硬件能够同时运行的数目相似--这在多处理器上比在单个处理器上更有可能失败。失败的最高风险是在AtomicBoolean变为假之后。所有可用的处理器都应该同时尝试获得对它的独占访问,将其视为false,并将其原子化地更改为true。
发布于 2013-07-01 23:57:04
正如您所指出的,我认为比AtomicInteger更难测试这一点。可能的价值空间要小得多,因此可能出错的东西的空间要小得多。因为像这样的测试基本上取决于运气(通过大量的循环来增加你的机会),实现这个较小的目标将更加困难。
我的建议是启动许多可以访问单个AtomicBoolean的线程。让他们每个人做一个CAS,并且只有当他们成功的时候,原子地增加一个AtomicInteger。当所有线程都完成时,您应该只看到该AtomicInteger的一个增量。然后冲洗,泡沫,重复。
发布于 2013-07-01 23:12:17
这是四个原子操作。假设你只想要一个布尔值与另一个布尔值相反,那么就有一个布尔值,然后继续切换。您可以从这个值计算另一个值。
https://stackoverflow.com/questions/17414924
复制相似问题