提交 e8448448 编写于 作者: C chegar

7021010: java/lang/Thread/ThreadStateTest.java fails intermittently

Reviewed-by: dholmes, alanb, mchung
上级 8616bf83
...@@ -207,9 +207,6 @@ java/lang/Runtime/exec/SleepyCat.java generic-all ...@@ -207,9 +207,6 @@ java/lang/Runtime/exec/SleepyCat.java generic-all
# Times out on solaris sparc -server # Times out on solaris sparc -server
java/lang/ThreadLocal/MemoryLeak.java solaris-all java/lang/ThreadLocal/MemoryLeak.java solaris-all
# Windows X64, RuntimeException: MyThread expected to have RUNNABLE but got WAITING
java/lang/Thread/ThreadStateTest.java generic-all
############################################################################ ############################################################################
# jdk_management # jdk_management
......
/* /*
* Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -34,21 +34,16 @@ ...@@ -34,21 +34,16 @@
*/ */
import java.util.concurrent.locks.LockSupport; import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.Semaphore; import java.util.concurrent.Phaser;
public class ThreadStateTest { public class ThreadStateTest {
// maximum number of retries when checking for thread state.
static final int MAX_RETRY = 500;
private static boolean testFailed = false; private static boolean testFailed = false;
static class Lock { // used to achieve waiting states
private String name; static final Object globalLock = new Object();
Lock(String name) {
this.name = name;
}
public String toString() {
return name;
}
}
private static Lock globalLock = new Lock("my lock");
public static void main(String[] argv) { public static void main(String[] argv) {
// Call Thread.getState to force all initialization done // Call Thread.getState to force all initialization done
...@@ -102,13 +97,27 @@ public class ThreadStateTest { ...@@ -102,13 +97,27 @@ public class ThreadStateTest {
System.out.println("Unexpected exception."); System.out.println("Unexpected exception.");
testFailed = true; testFailed = true;
} }
if (testFailed) if (testFailed)
throw new RuntimeException("TEST FAILED."); throw new RuntimeException("TEST FAILED.");
System.out.println("Test passed."); System.out.println("Test passed.");
} }
private static void checkThreadState(Thread t, Thread.State expected) { private static void checkThreadState(Thread t, Thread.State expected) {
Thread.State state = t.getState(); // wait for the thread to transition to the expected state.
// There is a small window between the thread checking the state
// and the thread actual entering that state.
Thread.State state;
int retryCount=0;
while ((state = t.getState()) != expected && retryCount < MAX_RETRY) {
if (state != Thread.State.RUNNABLE) {
throw new RuntimeException("Thread not in expected state yet," +
" but it should at least be RUNNABLE");
}
goSleep(10);
retryCount++;
}
System.out.println("Checking thread state " + state); System.out.println("Checking thread state " + state);
if (state == null) { if (state == null) {
throw new RuntimeException(t.getName() + " expected to have " + throw new RuntimeException(t.getName() + " expected to have " +
...@@ -121,13 +130,6 @@ public class ThreadStateTest { ...@@ -121,13 +130,6 @@ public class ThreadStateTest {
} }
} }
private static String getLockName(Object lock) {
if (lock == null) return null;
return lock.getClass().getName() + '@' +
Integer.toHexString(System.identityHashCode(lock));
}
private static void goSleep(long ms) { private static void goSleep(long ms) {
try { try {
Thread.sleep(ms); Thread.sleep(ms);
...@@ -139,7 +141,9 @@ public class ThreadStateTest { ...@@ -139,7 +141,9 @@ public class ThreadStateTest {
} }
static class MyThread extends Thread { static class MyThread extends Thread {
private ThreadExecutionSynchronizer thrsync = new ThreadExecutionSynchronizer(); // Phaser to sync between the main thread putting
// this thread into various states
private Phaser phaser = new Phaser(2);
MyThread(String name) { MyThread(String name) {
super(name); super(name);
...@@ -153,12 +157,14 @@ public class ThreadStateTest { ...@@ -153,12 +157,14 @@ public class ThreadStateTest {
private final int TIMED_PARKED = 5; private final int TIMED_PARKED = 5;
private final int SLEEPING = 6; private final int SLEEPING = 6;
private final int TERMINATE = 7; private final int TERMINATE = 7;
private int state = RUNNABLE;
private volatile int state = RUNNABLE;
private boolean done = false; private boolean done = false;
public void run() { public void run() {
// Signal main thread to continue. // Signal main thread to continue.
thrsync.signal(); phaser.arriveAndAwaitAdvance();
while (!done) { while (!done) {
switch (state) { switch (state) {
case RUNNABLE: { case RUNNABLE: {
...@@ -172,7 +178,7 @@ public class ThreadStateTest { ...@@ -172,7 +178,7 @@ public class ThreadStateTest {
} }
case BLOCKED: { case BLOCKED: {
// signal main thread. // signal main thread.
thrsync.signal(); phaser.arrive();
System.out.println(" myThread is going to block."); System.out.println(" myThread is going to block.");
synchronized (globalLock) { synchronized (globalLock) {
// finish blocking // finish blocking
...@@ -183,7 +189,7 @@ public class ThreadStateTest { ...@@ -183,7 +189,7 @@ public class ThreadStateTest {
case WAITING: { case WAITING: {
synchronized (globalLock) { synchronized (globalLock) {
// signal main thread. // signal main thread.
thrsync.signal(); phaser.arrive();
System.out.println(" myThread is going to wait."); System.out.println(" myThread is going to wait.");
try { try {
globalLock.wait(); globalLock.wait();
...@@ -196,7 +202,7 @@ public class ThreadStateTest { ...@@ -196,7 +202,7 @@ public class ThreadStateTest {
case TIMED_WAITING: { case TIMED_WAITING: {
synchronized (globalLock) { synchronized (globalLock) {
// signal main thread. // signal main thread.
thrsync.signal(); phaser.arrive();
System.out.println(" myThread is going to timed wait."); System.out.println(" myThread is going to timed wait.");
try { try {
globalLock.wait(10000); globalLock.wait(10000);
...@@ -208,7 +214,7 @@ public class ThreadStateTest { ...@@ -208,7 +214,7 @@ public class ThreadStateTest {
} }
case PARKED: { case PARKED: {
// signal main thread. // signal main thread.
thrsync.signal(); phaser.arrive();
System.out.println(" myThread is going to park."); System.out.println(" myThread is going to park.");
LockSupport.park(); LockSupport.park();
// give a chance for the main thread to block // give a chance for the main thread to block
...@@ -217,7 +223,7 @@ public class ThreadStateTest { ...@@ -217,7 +223,7 @@ public class ThreadStateTest {
} }
case TIMED_PARKED: { case TIMED_PARKED: {
// signal main thread. // signal main thread.
thrsync.signal(); phaser.arrive();
System.out.println(" myThread is going to timed park."); System.out.println(" myThread is going to timed park.");
long deadline = System.currentTimeMillis() + 10000*1000; long deadline = System.currentTimeMillis() + 10000*1000;
LockSupport.parkUntil(deadline); LockSupport.parkUntil(deadline);
...@@ -228,20 +234,19 @@ public class ThreadStateTest { ...@@ -228,20 +234,19 @@ public class ThreadStateTest {
} }
case SLEEPING: { case SLEEPING: {
// signal main thread. // signal main thread.
thrsync.signal(); phaser.arrive();
System.out.println(" myThread is going to sleep."); System.out.println(" myThread is going to sleep.");
try { try {
Thread.sleep(1000000); Thread.sleep(1000000);
} catch (InterruptedException e) { } catch (InterruptedException e) {
// finish sleeping // finish sleeping
interrupted();
} }
break; break;
} }
case TERMINATE: { case TERMINATE: {
done = true; done = true;
// signal main thread. // signal main thread.
thrsync.signal(); phaser.arrive();
break; break;
} }
default: default:
...@@ -249,69 +254,66 @@ public class ThreadStateTest { ...@@ -249,69 +254,66 @@ public class ThreadStateTest {
} }
} }
} }
public void waitUntilStarted() { public void waitUntilStarted() {
// wait for MyThread. // wait for MyThread.
thrsync.waitForSignal(); phaser.arriveAndAwaitAdvance();
goSleep(10);
} }
public void goBlocked() { public void goBlocked() {
System.out.println("Waiting myThread to go blocked."); System.out.println("Waiting myThread to go blocked.");
setState(BLOCKED); setState(BLOCKED);
// wait for MyThread to get blocked // wait for MyThread to get to a point just before being blocked
thrsync.waitForSignal(); phaser.arriveAndAwaitAdvance();
goSleep(20);
} }
public void goWaiting() { public void goWaiting() {
System.out.println("Waiting myThread to go waiting."); System.out.println("Waiting myThread to go waiting.");
setState(WAITING); setState(WAITING);
// wait for MyThread to wait on object. // wait for MyThread to get to just before wait on object.
thrsync.waitForSignal(); phaser.arriveAndAwaitAdvance();
goSleep(20);
} }
public void goTimedWaiting() { public void goTimedWaiting() {
System.out.println("Waiting myThread to go timed waiting."); System.out.println("Waiting myThread to go timed waiting.");
setState(TIMED_WAITING); setState(TIMED_WAITING);
// wait for MyThread timed wait call. // wait for MyThread to get to just before timed wait call.
thrsync.waitForSignal(); phaser.arriveAndAwaitAdvance();
goSleep(20);
} }
public void goParked() { public void goParked() {
System.out.println("Waiting myThread to go parked."); System.out.println("Waiting myThread to go parked.");
setState(PARKED); setState(PARKED);
// wait for MyThread state change to PARKED. // wait for MyThread to get to just before parked.
thrsync.waitForSignal(); phaser.arriveAndAwaitAdvance();
goSleep(20);
} }
public void goTimedParked() { public void goTimedParked() {
System.out.println("Waiting myThread to go timed parked."); System.out.println("Waiting myThread to go timed parked.");
setState(TIMED_PARKED); setState(TIMED_PARKED);
// wait for MyThread. // wait for MyThread to get to just before timed park.
thrsync.waitForSignal(); phaser.arriveAndAwaitAdvance();
goSleep(20);
} }
public void goSleeping() { public void goSleeping() {
System.out.println("Waiting myThread to go sleeping."); System.out.println("Waiting myThread to go sleeping.");
setState(SLEEPING); setState(SLEEPING);
// wait for MyThread. // wait for MyThread to get to just before sleeping
thrsync.waitForSignal(); phaser.arriveAndAwaitAdvance();
goSleep(20);
} }
public void terminate() { public void terminate() {
System.out.println("Waiting myThread to terminate."); System.out.println("Waiting myThread to terminate.");
setState(TERMINATE); setState(TERMINATE);
// wait for MyThread. // wait for MyThread to get to just before terminate
thrsync.waitForSignal(); phaser.arriveAndAwaitAdvance();
goSleep(20);
} }
private void setState(int newState) { private void setState(int newState) {
switch (state) { switch (state) {
case BLOCKED: case BLOCKED:
while (state == BLOCKED) { while (state == BLOCKED) {
goSleep(20); goSleep(10);
} }
state = newState; state = newState;
break; break;
...@@ -337,50 +339,4 @@ public class ThreadStateTest { ...@@ -337,50 +339,4 @@ public class ThreadStateTest {
} }
} }
} }
static class ThreadExecutionSynchronizer {
private boolean waiting;
private Semaphore semaphore;
public ThreadExecutionSynchronizer() {
semaphore = new Semaphore(1);
waiting = false;
}
// Synchronizes two threads execution points.
// Basically any thread could get scheduled to run and
// it is not possible to know which thread reaches expected
// execution point. So whichever thread reaches a execution
// point first wait for the second thread. When the second thread
// reaches the expected execution point will wake up
// the thread which is waiting here.
void stopOrGo() {
semaphore.acquireUninterruptibly(); // Thread can get blocked.
if (!waiting) {
waiting = true;
// Wait for second thread to enter this method.
while(!semaphore.hasQueuedThreads()) {
try {
Thread.sleep(20);
} catch (InterruptedException xx) {}
}
semaphore.release();
} else {
waiting = false;
semaphore.release();
}
}
// Wrapper function just for code readability.
void waitForSignal() {
stopOrGo();
}
void signal() {
stopOrGo();
}
}
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册