publicvoid checkException() { if (exe != null) { thrownew TestException("Exception thrown by other thread!", exe);
}
}
privatevoid setAction(Action a) { int stamp = action.getStamp(); // Wait for it to be HOLD before updating. while (!action.compareAndSet(Action.HOLD, a, stamp, stamp + 1)) {
stamp = action.getStamp();
}
}
publicsynchronizedvoid suspendWorker() throws Exception {
checkException(); if (runner == null) { thrownew TestException("We don't have any runner holding " + lock);
}
Suspension.suspend(runner);
}
public Object getWorkerContendedMonitor() throws Exception {
checkException(); if (runner == null) { returnnull;
} return getCurrentContendedMonitor(runner);
}
publicsynchronizedvoid DoLock() { if (IsLocked()) { thrownew Error("lock is already acquired or being acquired.");
} if (runner != null) { thrownew Error("Already have thread!");
}
runner = newThread(() -> {
started = true; try { synchronized (lock) {
held = true; int[] stamp_h = newint[] { -1 };
Action cur_action = Action.HOLD; try { while (true) {
cur_action = action.get(stamp_h); int stamp = stamp_h[0]; if (cur_action == Action.RELEASE) { // The other thread will deal with reseting action. break;
} try { switch (cur_action) { case HOLD: Thread.yield(); break; case NOTIFY:
lock.DoNotify(); break; case NOTIFY_ALL:
lock.DoNotifyAll(); break; case TIMED_WAIT:
lock.DoWait(timeout); break; case WAIT:
lock.DoWait(); break; default: thrownew Error("Unknown action " + action);
}
} finally { // reset action back to hold if it isn't something else.
action.compareAndSet(cur_action, Action.HOLD, stamp, stamp+1);
}
}
} catch (Exception e) { thrownew TestException("Got an error while performing action " + cur_action, e);
}
}
} finally {
held = false;
started = false;
}
}, "Locker thread " + cnt.getAndIncrement() + " for " + lock); // Make sure we can get any exceptions this throws.
runner.setUncaughtExceptionHandler((t, e) -> { exe = e; });
runner.start();
}
publicvoid waitForLockToBeHeld() throws Exception { while (true) { if (IsLocked() && Objects.equals(runner, Monitors.getObjectMonitorUsage(lock).owner)) { return;
}
}
}
publicsynchronizedvoid waitForNotifySleep() throws Exception { if (runner == null) { thrownew Error("No thread trying to lock!");
} do {
checkException();
} while (!started ||
!Arrays.asList(Monitors.getObjectMonitorUsage(lock).notifyWaiters).contains(runner));
}
publicsynchronizedvoid waitForContendedSleep() throws Exception { if (runner == null) { thrownew Error("No thread trying to lock!");
} do {
checkException();
} while (!started ||
runner.getState() != Thread.State.BLOCKED ||
!Arrays.asList(Monitors.getObjectMonitorUsage(lock).waiters).contains(runner));
}
publicsynchronizedvoid DoUnlock() throws Exception {
Error throwing = null; if (!IsLocked()) { // We might just be racing some exception that was thrown by the worker thread. Cache the // exception, we will throw one from the worker before this one.
throwing = new Error("Not locked!");
}
setAction(Action.RELEASE); Thread run = runner;
runner = null; while (held) {}
run.join();
action.set(Action.HOLD, 0); // Make sure to throw any exception that occurred since it might not have unlocked due to our // request.
checkException();
DoCleanup(); if (throwing != null) { throw throwing;
}
}
publicsynchronizedvoid DoCleanup() throws Exception { if (runner != null) { Thread run = runner;
runner = null; while (held) {}
run.join();
}
action.set(Action.HOLD, 0);
exe = null;
}
}
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.11 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.