Skip to content

Commit 1fda842

Browse files
author
Alan Bateman
committed
8291429: java/lang/Thread/virtual/ThreadAPI.java timed out on single core system
Reviewed-by: sspitsyn, cjplummer
1 parent 2d25c0a commit 1fda842

File tree

4 files changed

+61
-10
lines changed

4 files changed

+61
-10
lines changed

test/jdk/java/lang/Thread/virtual/ThreadAPI.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,7 @@ public void testJoin32() throws Exception {
754754

755755
/**
756756
* Test platform thread invoking timed-Thread.join on a thread that is parking
757-
* and unparking.
757+
* and unparking while pinned.
758758
*/
759759
@Test
760760
public void testJoin33() throws Exception {
@@ -775,11 +775,18 @@ public void testJoin33() throws Exception {
775775

776776
/**
777777
* Test virtual thread invoking timed-Thread.join on a thread that is parking
778-
* and unparking.
778+
* and unparking while pinned.
779779
*/
780780
@Test
781781
public void testJoin34() throws Exception {
782-
VThreadRunner.run(this::testJoin33);
782+
// need at least two carrier threads due to pinning
783+
int previousParallelism = VThreadRunner.ensureParallelism(2);
784+
try {
785+
VThreadRunner.run(this::testJoin33);
786+
} finally {
787+
// restore
788+
VThreadRunner.setParallelism(previousParallelism);
789+
}
783790
}
784791

785792
/**

test/jdk/java/lang/management/ThreadMXBean/VirtualThreadDeadlocks.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,24 @@
2222
*/
2323

2424
/**
25-
* @test
25+
* @test id=default
2626
* @bug 8284161 8287103
2727
* @summary Test ThredMXBean.findMonitorDeadlockedThreads with cycles of
2828
* platform and virtual threads in deadlock
2929
* @enablePreview
30-
* @modules java.management
30+
* @modules java.base/java.lang:+open java.management
31+
* @library /test/lib
3132
* @run main/othervm VirtualThreadDeadlocks PP
3233
* @run main/othervm VirtualThreadDeadlocks PV
3334
* @run main/othervm VirtualThreadDeadlocks VV
3435
*/
3536

3637
/**
37-
* @test
38+
* @test id=no-vmcontinuations
3839
* @requires vm.continuations
3940
* @enablePreview
40-
* @modules java.management
41+
* @modules java.base/java.lang:+open java.management
42+
* @library /test/lib
4143
* @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:-VMContinuations VirtualThreadDeadlocks PP
4244
* @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:-VMContinuations VirtualThreadDeadlocks PV
4345
* @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:-VMContinuations VirtualThreadDeadlocks VV
@@ -48,6 +50,7 @@
4850
import java.util.Arrays;
4951
import java.util.concurrent.CyclicBarrier;
5052
import java.util.stream.Stream;
53+
import jdk.test.lib.thread.VThreadRunner;
5154

5255
public class VirtualThreadDeadlocks {
5356
private static final Object LOCK1 = new Object();
@@ -61,6 +64,8 @@ public class VirtualThreadDeadlocks {
6164
* VV = test deadlock with two virtual threads
6265
*/
6366
public static void main(String[] args) throws Exception {
67+
// need at least two carrier threads due to pinning
68+
VThreadRunner.ensureParallelism(2);
6469

6570
// start thread1
6671
Thread.Builder builder1 = (args[0].charAt(0) == 'P')

test/jdk/java/lang/management/ThreadMXBean/VirtualThreads.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
*/
2323

2424
/**
25-
* @test
25+
* @test id=default
2626
* @bug 8284161 8290562
2727
* @summary Test java.lang.management.ThreadMXBean with virtual threads
2828
* @enablePreview
@@ -31,7 +31,7 @@
3131
*/
3232

3333
/**
34-
* @test
34+
* @test id=no-vmcontinuations
3535
* @requires vm.continuations
3636
* @enablePreview
3737
* @modules java.base/java.lang:+open java.management

test/lib/jdk/test/lib/thread/VThreadRunner.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@
2323

2424
package jdk.test.lib.thread;
2525

26+
import java.lang.reflect.Field;
2627
import java.time.Duration;
28+
import java.util.concurrent.ForkJoinPool;
2729
import java.util.concurrent.atomic.AtomicReference;
2830

2931
/**
30-
* Helper class for running tasks in a virtual thread.
32+
* Helper class to support tests running tasks a in virtual thread.
3133
*/
3234
public class VThreadRunner {
3335
private VThreadRunner() { }
@@ -140,4 +142,41 @@ public static void run(int characteristics, ThrowingRunnable task) throws Except
140142
public static void run(ThrowingRunnable task) throws Exception {
141143
run(null, 0, task);
142144
}
145+
146+
/**
147+
* Returns the virtual thread scheduler.
148+
*/
149+
private static ForkJoinPool defaultScheduler() {
150+
try {
151+
var clazz = Class.forName("java.lang.VirtualThread");
152+
var field = clazz.getDeclaredField("DEFAULT_SCHEDULER");
153+
field.setAccessible(true);
154+
return (ForkJoinPool) field.get(null);
155+
} catch (Exception e) {
156+
throw new RuntimeException(e);
157+
}
158+
}
159+
160+
/**
161+
* Sets the virtual thread scheduler's target parallelism.
162+
* @return the previous parallelism level
163+
*/
164+
public static int setParallelism(int size) {
165+
return defaultScheduler().setParallelism(size);
166+
}
167+
168+
/**
169+
* Ensures that the virtual thread scheduler's target parallelism is at least
170+
* the given size. If the target parallelism is less than the given size then
171+
* it is changed to the given size.
172+
* @return the previous parallelism level
173+
*/
174+
public static int ensureParallelism(int size) {
175+
ForkJoinPool pool = defaultScheduler();
176+
int parallelism = pool.getParallelism();
177+
if (size > parallelism) {
178+
pool.setParallelism(size);
179+
}
180+
return parallelism;
181+
}
143182
}

0 commit comments

Comments
 (0)