Fix SimpleAsyncTaskExecutor virtual thread path
This commit makes sure that the SimpleAsyncTaskExecutor only creates a virtual thread when the virtualThreadDelegate is set, and not an additional kernel thread as well. See gh-30241
This commit is contained in:
@@ -260,9 +260,10 @@ public class SimpleAsyncTaskExecutor extends CustomizableThreadCreator
|
|||||||
if (this.virtualThreadDelegate != null) {
|
if (this.virtualThreadDelegate != null) {
|
||||||
this.virtualThreadDelegate.startVirtualThread(nextThreadName(), task);
|
this.virtualThreadDelegate.startVirtualThread(nextThreadName(), task);
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
Thread thread = (this.threadFactory != null ? this.threadFactory.newThread(task) : createThread(task));
|
Thread thread = (this.threadFactory != null ? this.threadFactory.newThread(task) : createThread(task));
|
||||||
thread.start();
|
thread.start();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -16,6 +16,8 @@
|
|||||||
|
|
||||||
package org.springframework.core.task;
|
package org.springframework.core.task;
|
||||||
|
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
@@ -34,6 +36,7 @@ class VirtualThreadTaskExecutorTests {
|
|||||||
executeAndWait(executor, task, monitor);
|
executeAndWait(executor, task, monitor);
|
||||||
assertThat(task.getThreadName()).isEmpty();
|
assertThat(task.getThreadName()).isEmpty();
|
||||||
assertThat(task.isVirtual()).isTrue();
|
assertThat(task.isVirtual()).isTrue();
|
||||||
|
assertThat(task.runtCount()).isOne();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -44,6 +47,7 @@ class VirtualThreadTaskExecutorTests {
|
|||||||
executeAndWait(executor, task, monitor);
|
executeAndWait(executor, task, monitor);
|
||||||
assertThat(task.getThreadName()).isEqualTo("test-0");
|
assertThat(task.getThreadName()).isEqualTo("test-0");
|
||||||
assertThat(task.isVirtual()).isTrue();
|
assertThat(task.isVirtual()).isTrue();
|
||||||
|
assertThat(task.runtCount()).isOne();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -54,6 +58,7 @@ class VirtualThreadTaskExecutorTests {
|
|||||||
executeAndWait(executor, task, monitor);
|
executeAndWait(executor, task, monitor);
|
||||||
assertThat(task.getThreadName()).isEqualTo("test");
|
assertThat(task.getThreadName()).isEqualTo("test");
|
||||||
assertThat(task.isVirtual()).isTrue();
|
assertThat(task.isVirtual()).isTrue();
|
||||||
|
assertThat(task.runtCount()).isOne();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -66,6 +71,7 @@ class VirtualThreadTaskExecutorTests {
|
|||||||
executeAndWait(executor, task, monitor);
|
executeAndWait(executor, task, monitor);
|
||||||
assertThat(task.getThreadName()).startsWith(customPrefix);
|
assertThat(task.getThreadName()).startsWith(customPrefix);
|
||||||
assertThat(task.isVirtual()).isTrue();
|
assertThat(task.isVirtual()).isTrue();
|
||||||
|
assertThat(task.runtCount()).isOne();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void executeAndWait(TaskExecutor executor, Runnable task, Object monitor) {
|
private void executeAndWait(TaskExecutor executor, Runnable task, Object monitor) {
|
||||||
@@ -115,6 +121,8 @@ class VirtualThreadTaskExecutorTests {
|
|||||||
|
|
||||||
private static final class ThreadNameHarvester extends AbstractNotifyingRunnable {
|
private static final class ThreadNameHarvester extends AbstractNotifyingRunnable {
|
||||||
|
|
||||||
|
private final AtomicInteger runCount = new AtomicInteger();
|
||||||
|
|
||||||
private String threadName;
|
private String threadName;
|
||||||
|
|
||||||
private boolean virtual;
|
private boolean virtual;
|
||||||
@@ -131,11 +139,16 @@ class VirtualThreadTaskExecutorTests {
|
|||||||
return this.virtual;
|
return this.virtual;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int runtCount() {
|
||||||
|
return this.runCount.get();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void doRun() {
|
protected void doRun() {
|
||||||
Thread thread = Thread.currentThread();
|
Thread thread = Thread.currentThread();
|
||||||
this.threadName = thread.getName();
|
this.threadName = thread.getName();
|
||||||
this.virtual = thread.isVirtual();
|
this.virtual = thread.isVirtual();
|
||||||
|
runCount.incrementAndGet();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user