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; import java.util.concurrent.atomic.AtomicInteger;
public class AtomicIntegerTest { public class AtomicIntegerTest {
private static void runTest(final boolean increment, private static void runTest(final boolean increment,
final int threadCount, final int threadCount,
final int iterationsPerThread) { 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 result = new AtomicInteger();
final AtomicInteger threadDoneCount = 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++) { for (int i = 0; i < threadCount; i++) {
new Thread(new Runnable() { new Thread(new Runnable() {
@Override @Override
public void run() { public void run() {
try { try {
waitTillReady(); try {
waitTillReady();
} catch (InterruptedException e) {
// let thread exit
return;
}
doOperation(); doOperation();
} finally { } finally {
synchronized (threadDoneCount) { synchronized (threadDoneCount) {
@ -25,14 +31,10 @@ public class AtomicIntegerTest {
} }
} }
private void waitTillReady() { private void waitTillReady() throws InterruptedException {
long sleepTime = System.currentTimeMillis() - startTime; synchronized (threadsStart) {
if (sleepTime > 0) { while (! threadsStart.get()) {
try { threadsStart.wait();
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
// let thread exit
return;
} }
} }
} }
@ -60,6 +62,12 @@ public class AtomicIntegerTest {
}).start(); }).start();
} }
synchronized (threadsStart) {
threadsStart.set(true);
threadsStart.notifyAll();
}
synchronized (threadDoneCount) { synchronized (threadDoneCount) {
while (threadDoneCount.get() < threadCount) { while (threadDoneCount.get() < threadCount) {
try { try {

View File

@ -1,20 +1,26 @@
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
public class AtomicLongTest { public class AtomicLongTest {
private static void runTest(final boolean increment, private static void runTest(final boolean increment,
final int threadCount, final int threadCount,
final int iterationsPerThread) { 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 result = new AtomicLong();
final AtomicLong threadDoneCount = 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++) { for (int i = 0; i < threadCount; i++) {
new Thread(new Runnable() { new Thread(new Runnable() {
@Override @Override
public void run() { public void run() {
try { try {
waitTillReady(); try {
waitTillReady();
} catch (InterruptedException e) {
// let thread exit
return;
}
doOperation(); doOperation();
} finally { } finally {
synchronized (threadDoneCount) { synchronized (threadDoneCount) {
@ -25,14 +31,10 @@ public class AtomicLongTest {
} }
} }
private void waitTillReady() { private void waitTillReady() throws InterruptedException {
long sleepTime = System.currentTimeMillis() - startTime; synchronized (threadsStart) {
if (sleepTime > 0) { while (! threadsStart.get()) {
try { threadsStart.wait();
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
// let thread exit
return;
} }
} }
} }
@ -60,6 +62,12 @@ public class AtomicLongTest {
}).start(); }).start();
} }
synchronized (threadsStart) {
threadsStart.set(true);
threadsStart.notifyAll();
}
synchronized (threadDoneCount) { synchronized (threadDoneCount) {
while (threadDoneCount.get() < threadCount) { while (threadDoneCount.get() < threadCount) {
try { try {

View File

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