< prev index next > test/jdk/java/lang/Thread/virtual/VirtualThreadPinnedEventThrows.java
Print this page
/**
* @test
* @summary Test parking when pinned and emitting the JFR VirtualThreadPinnedEvent throws
* @modules java.base/jdk.internal.event
* @compile/module=java.base jdk/internal/event/VirtualThreadPinnedEvent.java
! * @run junit VirtualThreadPinnedEventThrows
*/
import java.lang.ref.Reference;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.LockSupport;
import jdk.internal.event.VirtualThreadPinnedEvent;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class VirtualThreadPinnedEventThrows {
/**
* @test
* @summary Test parking when pinned and emitting the JFR VirtualThreadPinnedEvent throws
* @modules java.base/jdk.internal.event
+ * @library /test/lib
* @compile/module=java.base jdk/internal/event/VirtualThreadPinnedEvent.java
! * @run junit/othervm --enable-native-access=ALL-UNNAMED VirtualThreadPinnedEventThrows
*/
import java.lang.ref.Reference;
import java.util.concurrent.atomic.AtomicBoolean;
+ import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.LockSupport;
import jdk.internal.event.VirtualThreadPinnedEvent;
+ import jdk.test.lib.thread.VThreadPinner;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class VirtualThreadPinnedEventThrows {
/**
* Test parking a virtual thread when pinned.
*/
private void testParkWhenPinned() throws Exception {
! Object lock = new Object();
try {
- var completed = new AtomicBoolean();
- Thread thread = Thread.startVirtualThread(() -> {
- synchronized (lock) {
- LockSupport.park();
- completed.set(true);
- }
- });
-
// wait for thread to park
Thread.State state;
while ((state = thread.getState()) != Thread.State.WAITING) {
assertTrue(state != Thread.State.TERMINATED);
Thread.sleep(10);
}
!
! // unpark and check that thread completed without exception
LockSupport.unpark(thread);
thread.join();
- assertTrue(completed.get());
- } finally {
- Reference.reachabilityFence(lock);
}
}
}
/**
* Test parking a virtual thread when pinned.
*/
private void testParkWhenPinned() throws Exception {
! var exception = new AtomicReference<Throwable>();
+ var done = new AtomicBoolean();
+ Thread thread = Thread.startVirtualThread(() -> {
+ try {
+ VThreadPinner.runPinned(() -> {
+ while (!done.get()) {
+ LockSupport.park();
+ }
+ });
+ } catch (Throwable e) {
+ exception.set(e);
+ }
+ });
try {
// wait for thread to park
Thread.State state;
while ((state = thread.getState()) != Thread.State.WAITING) {
assertTrue(state != Thread.State.TERMINATED);
Thread.sleep(10);
}
! } finally {
! done.set(true);
LockSupport.unpark(thread);
thread.join();
}
+ assertNull(exception.get());
}
}
< prev index next >