【虚拟线程】java21虚拟线程用法 限流等
ThreadFactory virtualThreadFactory = Thread.ofVirtual()
Semaphore 限流 = new Semaphore(10);
List threads = new ArrayList<>();
for(int i = 0; i < 100; i++){
Runnable runnable = () -> {
ThreadUtil.sleep(1000 * 5);
Thread factoryThread = virtualThreadFactory.newThread(runnable);
threads.add(factoryThread);
for (Thread thread : threads) {
方案二
ThreadFactory virtualThreadFactory = Thread.ofVirtual()
//线程池 引用com.alibaba.ttl.threadpool
return TtlExecutors.getTtlExecutorService(new ThreadPoolExecutor(核心数量, 最大数量, 60,
TimeUnit.SECONDS, new ArrayBlockingQueue<>(queueCapacity), virtualThreadFactory, new ThreadPoolExecutor.AbortPolicy()));
方案三
ExecutorService executorService = Executors.newVirtualThreadPerTaskExecutor();