Avoid doing a Thread.sleep() and instead do a wait and notify.

This commit is contained in:
Mike Jensen 2014-01-03 15:39:40 -07:00
parent 9809898470
commit 2760252a13
3 changed files with 57 additions and 33 deletions

View File

@ -1,20 +1,26 @@
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
public class AtomicIntegerTest {
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 + 10;
final AtomicInteger result = new AtomicInteger();
final AtomicInteger threadDoneCount = new AtomicInteger();
// only using an AtomicBoolean here so I don't need two variables to do the synchronize/wait/notify
final AtomicBoolean threadsStart = new AtomicBoolean(false);
for (int i = 0; i < threadCount; i++) {
new Thread(new Runnable() {
@Override
public void run() {
try {
waitTillReady();
try {
waitTillReady();
} catch (InterruptedException e) {
// let thread exit
return;
}
doOperation();
} finally {
synchronized (threadDoneCount) {
@ -25,14 +31,10 @@ public class AtomicIntegerTest {
}
}
private void waitTillReady() {
long sleepTime = System.currentTimeMillis() - startTime;
if (sleepTime > 0) {
try {
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
// let thread exit
return;
private void waitTillReady() throws InterruptedException {
synchronized (threadsStart) {
while (! threadsStart.get()) {
threadsStart.wait();
}
}
}
@ -60,6 +62,12 @@ public class AtomicIntegerTest {
}).start();
}
synchronized (threadsStart) {
threadsStart.set(true);
threadsStart.notifyAll();
}
synchronized (threadDoneCount) {
while (threadDoneCount.get() < threadCount) {
try {

View File

@ -1,20 +1,26 @@
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
public class AtomicLongTest {
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 + 10;
final AtomicLong result = new AtomicLong();
final AtomicLong threadDoneCount = new AtomicLong();
// only using an AtomicBoolean here so I don't need two variables to do the synchronize/wait/notify
final AtomicBoolean threadsStart = new AtomicBoolean(false);
for (int i = 0; i < threadCount; i++) {
new Thread(new Runnable() {
@Override
public void run() {
try {
waitTillReady();
try {
waitTillReady();
} catch (InterruptedException e) {
// let thread exit
return;
}
doOperation();
} finally {
synchronized (threadDoneCount) {
@ -25,14 +31,10 @@ public class AtomicLongTest {
}
}
private void waitTillReady() {
long sleepTime = System.currentTimeMillis() - startTime;
if (sleepTime > 0) {
try {
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
// let thread exit
return;
private void waitTillReady() throws InterruptedException {
synchronized (threadsStart) {
while (! threadsStart.get()) {
threadsStart.wait();
}
}
}
@ -60,6 +62,12 @@ public class AtomicLongTest {
}).start();
}
synchronized (threadsStart) {
threadsStart.set(true);
threadsStart.notifyAll();
}
synchronized (threadDoneCount) {
while (threadDoneCount.get() < threadCount) {
try {

View File

@ -1,20 +1,26 @@
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
public class AtomicReferenceTest {
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 + 10;
final AtomicReference<Integer> result = new AtomicReference<Integer>(0);
final AtomicInteger threadDoneCount = new AtomicInteger(0);
// only using an AtomicBoolean here so I don't need two variables to do the synchronize/wait/notify
final AtomicBoolean threadsStart = new AtomicBoolean(false);
for (int i = 0; i < threadCount; i++) {
new Thread(new Runnable() {
@Override
public void run() {
try {
waitTillReady();
try {
waitTillReady();
} catch (InterruptedException e) {
// let thread exit
return;
}
doOperation();
} finally {
synchronized (threadDoneCount) {
@ -25,14 +31,10 @@ public class AtomicReferenceTest {
}
}
private void waitTillReady() {
long sleepTime = System.currentTimeMillis() - startTime;
if (sleepTime > 0) {
try {
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
// let thread exit
return;
private void waitTillReady() throws InterruptedException {
synchronized (threadsStart) {
while (! threadsStart.get()) {
threadsStart.wait();
}
}
}
@ -48,6 +50,12 @@ public class AtomicReferenceTest {
}).start();
}
synchronized (threadsStart) {
threadsStart.set(true);
threadsStart.notifyAll();
}
synchronized (threadDoneCount) {
while (threadDoneCount.get() < threadCount) {
try {