MonitorTestpublic class MonitorTest extends TestCase
Fields Summary |
---|
private static Throwable | errorException | private static Thread | testThread | private static Object | commonLock | private static Boolean | running |
Methods Summary |
---|
private static void | deepWait(int depth, java.lang.Object lock)
synchronized (lock) {
if (depth > 0) {
deepWait(depth - 1, lock);
} else {
String threadName = Thread.currentThread().getName();
try {
// System.out.println(threadName + " waiting");
lock.wait();
// System.out.println(threadName + " done waiting");
} catch (InterruptedException ex) {
// System.out.println(threadName + " interrupted.");
}
}
}
| public void | testInterruptTest()
testThread = Thread.currentThread();
errorException = null;
Waiter waiter = new Waiter("InterruptTest Waiter");
Interrupter interrupter =
new Interrupter("InterruptTest Interrupter", waiter);
interrupter.start();
try {
interrupter.join();
waiter.join();
} catch (InterruptedException ex) {
throw new RuntimeException("Test join interrupted.", ex);
}
if (errorException != null) {
throw new RuntimeException("InterruptTest failed",
errorException);
}
| public void | testNestedMonitors()
final int NUM_WORKERS = 5;
Worker w[] = new Worker[NUM_WORKERS];
int i;
for (i = 0; i < NUM_WORKERS; i++) {
w[i] = new Worker(i * 2 - 1, new Object());
}
running = true;
// System.out.println("NestedMonitors: starting workers");
for (i = 0; i < NUM_WORKERS; i++) {
w[i].start();
}
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException ex) {
// System.out.println("Test sleep interrupted.");
}
for (i = 0; i < 100; i++) {
for (int j = 0; j < NUM_WORKERS; j++) {
synchronized (w[j].lock) {
w[j].lock.notify();
}
}
}
// System.out.println("NesterMonitors: stopping workers");
running = false;
for (i = 0; i < NUM_WORKERS; i++) {
synchronized (w[i].lock) {
w[i].lock.notifyAll();
}
}
| public void | testWaitArgumentsTest()
/* Try some valid arguments. These should all
* return very quickly.
*/
try {
synchronized (this) {
/* millisecond version */
wait(1);
wait(10);
/* millisecond + nanosecond version */
wait(0, 1);
wait(0, 999999);
wait(1, 1);
wait(1, 999999);
}
} catch (InterruptedException ex) {
throw new RuntimeException("good Object.wait() interrupted",
ex);
} catch (Exception ex) {
throw new RuntimeException("Unexpected exception when calling" +
"Object.wait() with good arguments", ex);
}
/* Try some invalid arguments.
*/
boolean sawException = false;
try {
synchronized (this) {
wait(-1);
}
} catch (InterruptedException ex) {
throw new RuntimeException("bad Object.wait() interrupted", ex);
} catch (IllegalArgumentException ex) {
sawException = true;
} catch (Exception ex) {
throw new RuntimeException("Unexpected exception when calling" +
"Object.wait() with bad arguments", ex);
}
if (!sawException) {
throw new RuntimeException("bad call to Object.wait() should " +
"have thrown IllegalArgumentException");
}
sawException = false;
try {
synchronized (this) {
wait(0, -1);
}
} catch (InterruptedException ex) {
throw new RuntimeException("bad Object.wait() interrupted", ex);
} catch (IllegalArgumentException ex) {
sawException = true;
} catch (Exception ex) {
throw new RuntimeException("Unexpected exception when calling" +
"Object.wait() with bad arguments", ex);
}
if (!sawException) {
throw new RuntimeException("bad call to Object.wait() should " +
"have thrown IllegalArgumentException");
}
sawException = false;
try {
synchronized (this) {
/* The legal range of nanos is 0-999999. */
wait(0, 1000000);
}
} catch (InterruptedException ex) {
throw new RuntimeException("bad Object.wait() interrupted", ex);
} catch (IllegalArgumentException ex) {
sawException = true;
} catch (Exception ex) {
throw new RuntimeException("Unexpected exception when calling" +
"Object.wait() with bad arguments", ex);
}
if (!sawException) {
throw new RuntimeException("bad call to Object.wait() should " +
"have thrown IllegalArgumentException");
}
|
|