Added some tests which would fail with a simple volatile, but should work for the atomic implementations.

This commit is contained in:
Mike Jensen 2014-01-03 09:36:27 -07:00
parent f7d77473a4
commit f7341732fc
3 changed files with 234 additions and 0 deletions

View File

@ -0,0 +1,83 @@
import java.util.concurrent.atomic.AtomicInteger;
public class AtomicIntegerConcurrentTest {
private static void runTest(final boolean increment,
final int threadCount,
final int iterationsPerThread) {
// we assume a 1ms delay per thread to try to get them all to start at the same time
final long startTime = System.currentTimeMillis() + threadCount;
final AtomicInteger result = new AtomicInteger();
final AtomicInteger threadDoneCount = new AtomicInteger();
for (int i = 0; i < threadCount; i++) {
new Thread(new Runnable() {
@Override
public void run() {
try {
doOperation();
} finally {
synchronized (threadDoneCount) {
threadDoneCount.incrementAndGet();
threadDoneCount.notifyAll();
}
}
}
private void doOperation() {
long sleepTime = System.currentTimeMillis() - startTime;
try {
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
// let thread exit
return;
}
boolean flip = true;
for (int i = 0; i < iterationsPerThread; i++) {
if (flip) {
if (increment) {
result.incrementAndGet();
} else {
result.decrementAndGet();
}
flip = false;
} else {
if (increment) {
result.getAndIncrement();
} else {
result.getAndDecrement();
}
flip = true;
}
}
}
}).start();
}
synchronized (threadDoneCount) {
while (threadDoneCount.get() < threadCount) {
try {
threadDoneCount.wait();
} catch (InterruptedException e) {
// let thread exit
return;
}
}
}
int expectedResult = threadCount * iterationsPerThread;
if (! increment) {
expectedResult *= -1;
}
int resultValue = result.get();
if (resultValue != expectedResult) {
throw new IllegalStateException(resultValue + " != " + expectedResult);
}
}
public static void main(String[] args) {
runTest(true, 10, 100);
runTest(false, 10, 100);
}
}

View File

@ -0,0 +1,83 @@
import java.util.concurrent.atomic.AtomicLong;
public class AtomicLongConcurrentTest {
private static void runTest(final boolean increment,
final int threadCount,
final int iterationsPerThread) {
// we assume a 1ms delay per thread to try to get them all to start at the same time
final long startTime = System.currentTimeMillis() + threadCount;
final AtomicLong result = new AtomicLong();
final AtomicLong threadDoneCount = new AtomicLong();
for (int i = 0; i < threadCount; i++) {
new Thread(new Runnable() {
@Override
public void run() {
try {
doOperation();
} finally {
synchronized (threadDoneCount) {
threadDoneCount.incrementAndGet();
threadDoneCount.notifyAll();
}
}
}
private void doOperation() {
long sleepTime = System.currentTimeMillis() - startTime;
try {
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
// let thread exit
return;
}
boolean flip = true;
for (int i = 0; i < iterationsPerThread; i++) {
if (flip) {
if (increment) {
result.incrementAndGet();
} else {
result.decrementAndGet();
}
flip = false;
} else {
if (increment) {
result.getAndIncrement();
} else {
result.getAndDecrement();
}
flip = true;
}
}
}
}).start();
}
synchronized (threadDoneCount) {
while (threadDoneCount.get() < threadCount) {
try {
threadDoneCount.wait();
} catch (InterruptedException e) {
// let thread exit
return;
}
}
}
long expectedResult = threadCount * iterationsPerThread;
if (! increment) {
expectedResult *= -1;
}
long resultValue = result.get();
if (resultValue != expectedResult) {
throw new IllegalStateException(resultValue + " != " + expectedResult);
}
}
public static void main(String[] args) {
runTest(true, 10, 100);
runTest(false, 10, 100);
}
}

View File

@ -0,0 +1,68 @@
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
public class AtomicReferenceConcurrentTest {
private static void runTest(final int threadCount,
final int iterationsPerThread) {
// we assume a 1ms delay per thread to try to get them all to start at the same time
final long startTime = System.currentTimeMillis() + threadCount;
final AtomicReference<Integer> result = new AtomicReference<Integer>(0);
final AtomicInteger threadDoneCount = new AtomicInteger(0);
for (int i = 0; i < threadCount; i++) {
new Thread(new Runnable() {
@Override
public void run() {
try {
doOperation();
} finally {
synchronized (threadDoneCount) {
threadDoneCount.incrementAndGet();
threadDoneCount.notifyAll();
}
}
}
private void doOperation() {
long sleepTime = System.currentTimeMillis() - startTime;
try {
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
// let thread exit
return;
}
for (int i = 0; i < iterationsPerThread; i++) {
Integer current = result.get();
while (! result.compareAndSet(current, current + 1)) {
current = result.get();
}
}
}
}).start();
}
synchronized (threadDoneCount) {
while (threadDoneCount.get() < threadCount) {
try {
threadDoneCount.wait();
} catch (InterruptedException e) {
// let thread exit
return;
}
}
}
long expectedResult = threadCount * iterationsPerThread;
Integer resultValue = result.get();
if (resultValue != expectedResult) {
throw new IllegalStateException(resultValue + " != " + expectedResult);
}
}
public static void main(String[] args) {
runTest(10, 100);
runTest(10, 100);
}
}