/** *TestThread.yieldsubmitsthetaskforthecurrentvirtualthreadtoascheduler *submissionqueuewhentherearenotasksinthelocalqueue.
*/
@Test void testYieldWithEmptyLocalQueue() throws Exception { var list = new CopyOnWriteArrayList<String>();
var threadsStarted = new AtomicBoolean();
var threadA = Thread.ofVirtual().unstarted(() -> { // pin thread until task for B is in submission queue while (!threadsStarted.get()) { Thread.onSpinWait();
}
list.add("A"); Thread.yield(); // push task for A to submission queue, B should run
list.add("A");
});
var threadB = Thread.ofVirtual().unstarted(() -> {
list.add("B");
});
// push tasks for A and B to submission queue
threadA.start();
threadB.start();
// release A
threadsStarted.set(true);
// wait for result
threadA.join();
threadB.join();
assertEquals(list, List.of("A", "B", "A"));
}
/** *TestThread.yieldsubmitsthetaskforthecurrentvirtualthreadtothelocal *queuewhentherearetasksinthelocalqueue.
*/
@Test void testYieldWithNonEmptyLocalQueue() throws Exception { var list = new CopyOnWriteArrayList<String>();
var threadsStarted = new AtomicBoolean();
var threadA = Thread.ofVirtual().unstarted(() -> { // pin thread until tasks for B and C are in submission queue while (!threadsStarted.get()) { Thread.onSpinWait();
}
list.add("A");
LockSupport.park(); // B should run
list.add("A");
});
var threadB = Thread.ofVirtual().unstarted(() -> {
list.add("B");
LockSupport.unpark(threadA); // push task for A to local queue Thread.yield(); // push task for B to local queue, A should run
list.add("B");
});
var threadC = Thread.ofVirtual().unstarted(() -> {
list.add("C");
});
// push tasks for A, B and C to submission queue
threadA.start();
threadB.start();
threadC.start();
// release A
threadsStarted.set(true);
// wait for result
threadA.join();
threadB.join();
threadC.join();
assertEquals(list, List.of("A", "B", "A", "B", "C"));
}
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.11 Sekunden
(vorverarbeitet am 2026-10-11)
¤
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.