// 两种写法
Thread.startVirtualThread(runnable).join();
Thread.ofVirtual().name("virtual-thread").start(runnable).join();
// 虚拟线程工厂
ThreadFactory virtualThreadFactory = Thread.ofVirtual().name("v").factory();
virtualThreadFactory.newThread(runnable).start();
// 第三种写法
try (ExecutorService executorService = Executors.newVirtualThreadPerTaskExecutor()){
IntStream.range(0,1000).forEach(i->{
executorService.execute(()->{
try {
Thread.sleep(Duration.ofSeconds(1));
System.out.println(i);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
});
});