publicclass Parking { privatestaticfinal Object lock = new Object();
/** *Park,unparkedbyplatformthread.
*/
@Test publicvoid testPark1() throws Exception { varthread = Thread.ofVirtual().start(LockSupport::park); Thread.sleep(1000); // give time for virtual thread to park
LockSupport.unpark(thread); thread.join();
}
/** *Park,unparkedbyvirtualthread.
*/
@Test publicvoid testPark2() throws Exception { var thread1 = Thread.ofVirtual().start(LockSupport::park); Thread.sleep(1000); // give time for virtual thread to park var thread2 = Thread.ofVirtual().start(() -> LockSupport.unpark(thread1));
thread1.join();
thread2.join();
}
/** *Parkwhileholdingmonitor,unparkedbyplatformthread.
*/
@Test publicvoid testPark3() throws Exception { varthread = Thread.ofVirtual().start(() -> { synchronized (lock) {
LockSupport.park();
}
}); Thread.sleep(1000); // give time for virtual thread to park
LockSupport.unpark(thread); thread.join();
}
/** *2xunparkbeforepark.
*/
@Test publicvoid testPark6() throws Exception { varthread = Thread.ofVirtual().start(() -> { Thread me = Thread.currentThread();
LockSupport.unpark(me);
LockSupport.unpark(me);
LockSupport.park();
LockSupport.park(); // should park
}); Thread.sleep(1000); // give time for thread to park
LockSupport.unpark(thread); thread.join();
}
/** *parkNanos(1000ms)parksthread.
*/
@Test publicvoid testParkNanos3() throws Exception {
VThreadRunner.run(() -> { // park for 1000ms long nanos = TimeUnit.NANOSECONDS.convert(1000, TimeUnit.MILLISECONDS); long start = System.nanoTime();
LockSupport.parkNanos(nanos);
// check that virtual thread parked for >= 900ms long elapsed = TimeUnit.MILLISECONDS.convert(System.nanoTime() - start,
TimeUnit.NANOSECONDS);
assertTrue(elapsed >= 900);
});
}
/** *ParkwithparkNanos,unparkedbyplatformthread.
*/
@Test publicvoid testParkNanos4() throws Exception { varthread = Thread.ofVirtual().start(() -> { long nanos = TimeUnit.NANOSECONDS.convert(1, TimeUnit.DAYS);
LockSupport.parkNanos(nanos);
}); Thread.sleep(100); // give time for virtual thread to park
LockSupport.unpark(thread); thread.join();
}
/** *ParkwithparkNanos,unparkedbyvirtualthread.
*/
@Test publicvoid testParkNanos5() throws Exception { var thread1 = Thread.ofVirtual().start(() -> { long nanos = TimeUnit.NANOSECONDS.convert(1, TimeUnit.DAYS);
LockSupport.parkNanos(nanos);
}); Thread.sleep(100); // give time for virtual thread to park var thread2 = Thread.ofVirtual().start(() -> LockSupport.unpark(thread1));
thread1.join();
thread2.join();
}
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.