System.out.println("Testing monitor timed with timeout.");
testTimedWaitTimeout(new Monitors.NamedLock("Lock testTimedWaitTimeout"));
// TODO It would be good (but annoying) to do this with jasmin/smali in order to test if it's // different without the reflection.
System.out.println("Waiting on an unlocked monitor.");
testUnlockedWait(new Monitors.NamedLock("Lock testUnlockedWait"));
System.out.println("Waiting with an illegal argument (negative timeout)");
testIllegalWait(new Monitors.NamedLock("Lock testIllegalWait"));
System.out.println("Interrupt a monitor being waited on.");
testInteruptWait(new Monitors.NamedLock("Lock testInteruptWait"));
}
publicstaticvoid testPark(Object blocker) throws Exception { Thread holder = newThread(() -> {
LockSupport.parkNanos(blocker, 10); // Should round up to one millisecond
}, "ParkThread");
holder.start();
holder.join();
}
publicstaticvoid testLock(Monitors.NamedLock lk) throws Exception {
Monitors.LockController controller1 = new Monitors.LockController(lk);
Monitors.LockController controller2 = new Monitors.LockController(lk);
controller1.DoLock();
controller1.waitForLockToBeHeld();
controller2.DoLock(); if (controller2.IsLocked()) { thrownew Exception("c2 was able to gain lock while it was held by c1");
}
controller2.waitForContendedSleep();
controller1.DoUnlock();
controller2.waitForLockToBeHeld();
controller2.DoUnlock();
}
publicstaticvoid testWait(Monitors.NamedLock lk) throws Exception {
Monitors.LockController controller1 = new Monitors.LockController(lk);
Monitors.LockController controller2 = new Monitors.LockController(lk);
controller1.DoLock();
controller1.waitForLockToBeHeld();
controller1.DoWait();
controller1.waitForNotifySleep(); try (AutoCloseable suppress = SuppressContention(controller2)) { // If controller1 has a spurious wakeup we could see contention here. Suppress it so it won't // cause the test to fail.
controller2.DoLock();
controller2.waitForLockToBeHeld();
controller2.DoNotifyAll();
controller2.DoUnlock();
}
controller1.waitForLockToBeHeld();
controller1.DoUnlock();
}
publicstaticvoid testTimedWait(Monitors.NamedLock lk) throws Exception { // Time to wait (1 hour). We will wake it up before timeout. finallong millis = 60l * 60l * 1000l;
Monitors.LockController controller1 = new Monitors.LockController(lk, millis);
Monitors.LockController controller2 = new Monitors.LockController(lk);
controller1.DoLock();
controller1.waitForLockToBeHeld();
controller1.DoTimedWait();
controller1.waitForNotifySleep(); try (AutoCloseable suppress = SuppressContention(controller2)) { // If controller1 has a spurious wakeup we could see contention here. Suppress it so it won't // cause the test to fail.
controller2.DoLock();
controller2.waitForLockToBeHeld();
controller2.DoNotifyAll();
controller2.DoUnlock();
}
controller1.waitForLockToBeHeld();
controller1.DoUnlock();
}
publicstaticvoid testTimedWaitTimeout(Monitors.NamedLock lk) throws Exception { // Time to wait (10 seconds). We will wait for the timeout. finallong millis = 10l * 1000l;
Monitors.LockController controller1 = new Monitors.LockController(lk, millis);
controller1.DoLock();
controller1.waitForLockToBeHeld();
System.out.println("Waiting for 10 seconds.");
controller1.DoTimedWait();
controller1.waitForNotifySleep();
controller1.DoUnlock();
System.out.println("Wait finished with timeout.");
}
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.9 Sekunden
(vorverarbeitet am 2026-06-29)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.