Fix for unit test failure when threads are created too slowly.

This commit is contained in:
Mike Jensen 2014-01-03 10:08:36 -07:00
parent f7341732fc
commit f4f4b8a26b
3 changed files with 39 additions and 25 deletions

View File

@ -5,7 +5,7 @@ public class AtomicIntegerConcurrentTest {
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 long startTime = System.currentTimeMillis() + threadCount + 10;
final AtomicInteger result = new AtomicInteger();
final AtomicInteger threadDoneCount = new AtomicInteger();
@ -15,6 +15,7 @@ public class AtomicIntegerConcurrentTest {
public void run() {
try {
doOperation();
waitTillReady();
} finally {
synchronized (threadDoneCount) {
threadDoneCount.incrementAndGet();
@ -24,15 +25,19 @@ public class AtomicIntegerConcurrentTest {
}
}
private void doOperation() {
private void waitTillReady() {
long sleepTime = System.currentTimeMillis() - startTime;
if (sleepTime > 0) {
try {
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
// let thread exit
return;
}
}
}
private void doOperation() {
boolean flip = true;
for (int i = 0; i < iterationsPerThread; i++) {
if (flip) {

View File

@ -5,7 +5,7 @@ public class AtomicLongConcurrentTest {
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 long startTime = System.currentTimeMillis() + threadCount + 10;
final AtomicLong result = new AtomicLong();
final AtomicLong threadDoneCount = new AtomicLong();
@ -15,6 +15,7 @@ public class AtomicLongConcurrentTest {
public void run() {
try {
doOperation();
waitTillReady();
} finally {
synchronized (threadDoneCount) {
threadDoneCount.incrementAndGet();
@ -24,15 +25,19 @@ public class AtomicLongConcurrentTest {
}
}
private void doOperation() {
private void waitTillReady() {
long sleepTime = System.currentTimeMillis() - startTime;
if (sleepTime > 0) {
try {
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
// let thread exit
return;
}
}
}
private void doOperation() {
boolean flip = true;
for (int i = 0; i < iterationsPerThread; i++) {
if (flip) {

View File

@ -5,7 +5,7 @@ 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 long startTime = System.currentTimeMillis() + threadCount + 10;
final AtomicReference<Integer> result = new AtomicReference<Integer>(0);
final AtomicInteger threadDoneCount = new AtomicInteger(0);
@ -15,6 +15,7 @@ public class AtomicReferenceConcurrentTest {
public void run() {
try {
doOperation();
waitTillReady();
} finally {
synchronized (threadDoneCount) {
threadDoneCount.incrementAndGet();
@ -24,15 +25,19 @@ public class AtomicReferenceConcurrentTest {
}
}
private void doOperation() {
private void waitTillReady() {
long sleepTime = System.currentTimeMillis() - startTime;
if (sleepTime > 0) {
try {
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
// let thread exit
return;
}
}
}
private void doOperation() {
for (int i = 0; i < iterationsPerThread; i++) {
Integer current = result.get();
while (! result.compareAndSet(current, current + 1)) {
@ -63,6 +68,5 @@ public class AtomicReferenceConcurrentTest {
public static void main(String[] args) {
runTest(10, 100);
runTest(10, 100);
}
}