publicfinalstaticclass ThreadSignaler { publicvolatileboolean signal = false;
}
privatestaticvoid testLockWait() throws Exception { final Monitors.NamedLock lk = new Monitors.NamedLock("Test1930 - testLockWait"); final Semaphore sem = new Semaphore(0); finalThread t = newThread(() -> {
sem.release(); synchronized (lk) {
printMonitorUsage(lk);
}
}, "Test1930 Thread - testLockWait"); synchronized (lk) {
t.start(); // Wait for the other thread to actually start.
sem.acquire(); // Wait for the other thread to go to sleep trying to get the mutex. This might take a (short) // time since we try spinning first for better performance. boolean found_wait = false; for (long i = 0; i < NUM_RETRY; i++) { if (Arrays.asList(Monitors.getObjectMonitorUsage(lk).waiters).contains(t)) {
found_wait = true; break;
} else { Thread.sleep(500); Thread.yield();
}
} if (!found_wait) {
System.out.println("other thread doesn't seem to be waiting.");
}
printMonitorUsage(lk);
}
t.join();
printMonitorUsage(lk);
}
publicstaticvoid run() throws Exception { // Single threaded tests.
System.out.println("Running with single thread.");
testSingleThread();
System.out.println("Running with single thread in native.");
testSingleThreadNative();
System.out.println("Lock twice");
testLockedTwice();
System.out.println("Lock twice native");
testLockedTwiceNative();
System.out.println("Lock twice Java then native");
testLockedTwiceJN();
System.out.println("Lock twice native then Java");
testLockedTwiceNJ();
// Mutli threaded tests.
System.out.println("lock with wait");
testLockWait();
System.out.println("Wait for notify.");
testNotifyWait();
}
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.