我们都推荐使用自己创建的线程池的方法;
测试如下:
package com.elasticsearch.thread;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ThreadTest {
public static ExecutorService service = Executors.newFixedThreadPool(5);
public static void main(String[] args) throws ExecutionException, InterruptedException {
System.out.println("main-----start");
// CompletableFuture.runAsync(()->{
// System.out.println("当前线程::" + Thread.currentThread().getId());
// int i = 10/2;
// System.out.println("运行结果::" + i);
// }, service);
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
System.out.println("当前线程::" + Thread.currentThread().getId());
int i = 10/2;
System.out.println("运行结果::" + i);
return i;
}, service);
System.out.println("main-----end--" + future.get());
}
}
标签:异步,java,System,---,CompletableFuture,println,out
From: https://www.cnblogs.com/morehair/p/17098551.html