代码
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class OutMaven {
private static ExecutorService executorService = Executors.newFixedThreadPool(3);
public String getName() {
return "OutMaven:" + System.currentTimeMillis();
}
public void sleep(int time) {
try {
Thread.sleep(time);
System.out.println("sleep:" + time);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws InterruptedException {
OutMaven outMaven = new OutMaven();
long l = System.currentTimeMillis();
int last = 2000;
CompletableFuture.allOf(CompletableFuture.runAsync(() -> {
outMaven.sleep(3000);
}, executorService), CompletableFuture.runAsync(() -> {
outMaven.sleep(4000);
}, executorService), CompletableFuture.runAsync(() -> {
outMaven.sleep(last);
}, executorService)).join();
long e = System.currentTimeMillis();
System.out.println("--111--" + (e - l));
executorService.shutdown();
l = System.currentTimeMillis();
CompletableFuture.allOf(CompletableFuture.runAsync(() -> {
outMaven.sleep(3000);
}), CompletableFuture.runAsync(() -> {
outMaven.sleep(4000);
}), CompletableFuture.runAsync(() -> {
outMaven.sleep(3000);
})).join();
e = System.currentTimeMillis();
System.out.println("--222--" + (e - l));
}
}