38 * @enablePreview
39 * @run junit/othervm -XX:+UseHeavyMonitors HoldsLock
40 */
41
42 import java.lang.management.LockInfo;
43 import java.lang.management.ManagementFactory;
44 import java.lang.management.ThreadInfo;
45 import java.lang.management.ThreadMXBean;
46 import java.util.Arrays;
47 import java.util.concurrent.ArrayBlockingQueue;
48 import java.util.concurrent.BlockingQueue;
49 import java.util.concurrent.ExecutionException;
50 import java.util.concurrent.Executor;
51 import java.util.concurrent.RejectedExecutionException;
52 import java.util.concurrent.ThreadFactory;
53 import java.util.concurrent.atomic.AtomicReference;
54
55 import org.junit.jupiter.api.Test;
56 import org.junit.jupiter.api.Disabled;
57 import static org.junit.jupiter.api.Assertions.*;
58
59 class HoldsLock {
60 static final Object LOCK1 = new Object();
61 static final Object LOCK2 = new Object();
62
63 @Disabled("JDK-8281642")
64 @Test
65 void testHoldsLock() throws Exception {
66 var q = new ArrayBlockingQueue<Runnable>(5);
67
68 Thread carrier = Thread.ofPlatform().start(() -> {
69 synchronized (LOCK1) {
70 eventLoop(q);
71 }
72 });
73
74 var ex = new AtomicReference<Throwable>();
75 Thread vthread = spawnVirtual(ex, executor(q), () -> {
76 assertTrue(Thread.currentThread().isVirtual());
77 assertFalse(carrier.isVirtual());
78
79 synchronized (LOCK2) {
80 assertTrue(Thread.holdsLock(LOCK2)); // virtual thread holds lock2
81 assertFalse(Thread.holdsLock(LOCK1)); // carrier thread holds lock1
82 }
83 });
84
85 join(vthread, ex);
86 stop(carrier);
87 }
88
89 @Test
90 void testThreadInfo() throws Exception {
91 var q = new ArrayBlockingQueue<Runnable>(5);
92
93 Thread carrier = spawnCarrier(q);
94 Thread vthread = spawnVirtual(executor(q), () -> {
95 synchronized (LOCK1) {
96 try {
97 LOCK1.wait();
98 } catch (InterruptedException e) {}
99 }
100 });
101
102 while (vthread.getState() != Thread.State.WAITING) {
103 Thread.sleep(10);
104 }
105 System.out.format("%s is waiting on %s%n", vthread, LOCK1);
106 long vthreadId = vthread.getId();
107 long carrierId = carrier.getId();
108
109 System.out.format("\t\t%s%n", LOCK1);
110 String lockAsString = LOCK1.toString();
|
38 * @enablePreview
39 * @run junit/othervm -XX:+UseHeavyMonitors HoldsLock
40 */
41
42 import java.lang.management.LockInfo;
43 import java.lang.management.ManagementFactory;
44 import java.lang.management.ThreadInfo;
45 import java.lang.management.ThreadMXBean;
46 import java.util.Arrays;
47 import java.util.concurrent.ArrayBlockingQueue;
48 import java.util.concurrent.BlockingQueue;
49 import java.util.concurrent.ExecutionException;
50 import java.util.concurrent.Executor;
51 import java.util.concurrent.RejectedExecutionException;
52 import java.util.concurrent.ThreadFactory;
53 import java.util.concurrent.atomic.AtomicReference;
54
55 import org.junit.jupiter.api.Test;
56 import org.junit.jupiter.api.Disabled;
57 import static org.junit.jupiter.api.Assertions.*;
58 import static org.junit.jupiter.api.Assumptions.*;
59
60 class HoldsLock {
61 static final Object LOCK1 = new Object();
62 static final Object LOCK2 = new Object();
63
64 @Disabled("JDK-8281642")
65 @Test
66 void testHoldsLock() throws Exception {
67 var q = new ArrayBlockingQueue<Runnable>(5);
68
69 Thread carrier = Thread.ofPlatform().start(() -> {
70 synchronized (LOCK1) {
71 eventLoop(q);
72 }
73 });
74
75 var ex = new AtomicReference<Throwable>();
76 Thread vthread = spawnVirtual(ex, executor(q), () -> {
77 assertTrue(Thread.currentThread().isVirtual());
78 assertFalse(carrier.isVirtual());
79
80 synchronized (LOCK2) {
81 assertTrue(Thread.holdsLock(LOCK2)); // virtual thread holds lock2
82 assertFalse(Thread.holdsLock(LOCK1)); // carrier thread holds lock1
83 }
84 });
85
86 join(vthread, ex);
87 stop(carrier);
88 }
89
90 @Test
91 void testThreadInfo() throws Exception {
92 assumeFalse(Thread.currentThread().isVirtual(), "Main thread must be platform thread");
93
94 var q = new ArrayBlockingQueue<Runnable>(5);
95
96 Thread carrier = spawnCarrier(q);
97 Thread vthread = spawnVirtual(executor(q), () -> {
98 synchronized (LOCK1) {
99 try {
100 LOCK1.wait();
101 } catch (InterruptedException e) {}
102 }
103 });
104
105 while (vthread.getState() != Thread.State.WAITING) {
106 Thread.sleep(10);
107 }
108 System.out.format("%s is waiting on %s%n", vthread, LOCK1);
109 long vthreadId = vthread.getId();
110 long carrierId = carrier.getId();
111
112 System.out.format("\t\t%s%n", LOCK1);
113 String lockAsString = LOCK1.toString();
|