update 优化 增加线程工具简化虚拟线程语法

This commit is contained in:
疯狂的狮子Li
2026-01-22 18:01:45 +08:00
parent 660757cb71
commit b8571e9ca1
2 changed files with 47 additions and 18 deletions

View File

@@ -0,0 +1,36 @@
package org.dromara.common.core.utils;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
/**
* 线程工具
*
* @author Lion Li
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ThreadUtils {
/**
* 批量执行任务
*/
public static void virtualSubmit(Runnable ...runnableList) {
List<Future<?>> callableList = new ArrayList<>();
try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) {
for (Runnable runnable : runnableList) {
callableList.add(executor.submit(runnable));
}
for (Future<?> future : callableList) {
future.get();
}
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}
}
}