6815130: Intermittent ThreadMXBean/Locks.java test failure

Preventing stale reads from ThreadExecutionSynchronizer.waiting flag

Reviewed-by: dholmes, mchung, dfuchs
This commit is contained in:
Jaroslav Bachorik 2013-09-06 10:03:16 +02:00
parent 37709e3191
commit 61000f0c4b
2 changed files with 14 additions and 15 deletions

View File

@ -193,16 +193,18 @@ public class Locks {
public CheckerThread() { public CheckerThread() {
super("CheckerThread"); super("CheckerThread");
} }
private void waitForState(Thread.State state) {
thrsync.waitForSignal();
while (waiter.getState() != state) {
goSleep(10);
}
}
public void run() { public void run() {
synchronized (ready) { synchronized (ready) {
// wait until WaitingThread about to wait for objC // wait until WaitingThread about to wait for objC
thrsync.waitForSignal(); waitForState(Thread.State.WAITING);
int retryCount = 0;
while (waiter.getState() != Thread.State.WAITING
&& retryCount++ < 500) {
goSleep(100);
}
checkBlockedObject(waiter, objC, null, Thread.State.WAITING); checkBlockedObject(waiter, objC, null, Thread.State.WAITING);
synchronized (objC) { synchronized (objC) {
@ -211,16 +213,13 @@ public class Locks {
// wait for waiter thread to about to enter // wait for waiter thread to about to enter
// synchronized object ready. // synchronized object ready.
thrsync.waitForSignal(); waitForState(Thread.State.BLOCKED);
// give chance for waiter thread to get blocked on
// object ready.
goSleep(50);
checkBlockedObject(waiter, ready, this, Thread.State.BLOCKED); checkBlockedObject(waiter, ready, this, Thread.State.BLOCKED);
} }
// wait for signal from waiting thread that it is about // wait for signal from waiting thread that it is about
// wait for objC. // wait for objC.
thrsync.waitForSignal(); waitForState(Thread.State.WAITING);
synchronized(objC) { synchronized(objC) {
checkBlockedObject(waiter, objC, Thread.currentThread(), Thread.State.WAITING); checkBlockedObject(waiter, objC, Thread.currentThread(), Thread.State.WAITING);
objC.notify(); objC.notify();

View File

@ -23,7 +23,7 @@
/* /*
* *
* @summary Thiseclass is used to synchronize execution off two threads. * @summary This class is used to synchronize execution of two threads.
* @author Swamy Venkataramanappa * @author Swamy Venkataramanappa
*/ */
@ -31,8 +31,8 @@ import java.util.concurrent.Semaphore;
public class ThreadExecutionSynchronizer { public class ThreadExecutionSynchronizer {
private boolean waiting; private volatile boolean waiting;
private Semaphore semaphore; private final Semaphore semaphore;
public ThreadExecutionSynchronizer() { public ThreadExecutionSynchronizer() {
semaphore = new Semaphore(1); semaphore = new Semaphore(1);