原创转载请注明出处:http://agilestyle.iteye.com/blog/2427053
completableFuture.get()
package org.fool.java8.completable;
import java.util.Optional;
import java.util.Random;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
public class CompletableFutureTest0 {
private final static Random RANDOM = new Random(System.currentTimeMillis());
public static void main(String[] args) throws ExecutionException, InterruptedException {
CompletableFuture<Double> completableFuture = new CompletableFuture<>();
new Thread(() -> {
double result = get();
completableFuture.complete(result);
}).start();
System.out.println("=========main start no block==========");
Optional.ofNullable(completableFuture.get()).ifPresent(System.out::println);
System.out.println("=========main end no block==========");
}
private static double get() {
try {
Thread.sleep(RANDOM.nextInt(10000));
} catch (InterruptedException e) {
e.printStackTrace();
}
return RANDOM.nextDouble();
}
}
Console Output
Note: completableFuture.get() 是阻塞的
CompletableFuture<T> whenComplete(BiConsumer<? super T, ? super Throwable> action)
package org.fool.java8.completable;
import java.util.Optional;
import java.util.Random;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
public class CompletableFutureTest1 {
private final static Random RANDOM = new Random(System.currentTimeMillis());
public static void main(String[] args) throws ExecutionException, InterruptedException {
CompletableFuture<Double> completableFuture = new CompletableFuture<>();
new Thread(() -> {
double result = get();
completableFuture.complete(result);
}).start();
System.out.println("=========main start no block==========");
completableFuture.whenComplete((v, t) -> {
Optional.ofNullable(v).ifPresent(System.out::println);
Optional.ofNullable(t).ifPresent(Throwable::printStackTrace);
});
System.out.println("=========main end no block==========");
}
private static double get() {
try {
Thread.sleep(RANDOM.nextInt(10000));
} catch (InterruptedException e) {
e.printStackTrace();
}
return RANDOM.nextDouble();
}
}
Console Output
Note: completableFuture.whenComplete 是非阻塞的
<U> CompletableFuture<U> supplyAsync(Supplier<U> supplier)
package org.fool.java8.completable;
import java.util.Optional;
import java.util.Random;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicBoolean;
public class CompletableFutureTest2 {
private final static Random RANDOM = new Random(System.currentTimeMillis());
public static void main(String[] args) throws ExecutionException, InterruptedException {
AtomicBoolean finished = new AtomicBoolean(false);
CompletableFuture.supplyAsync(CompletableFutureTest2::get).whenComplete((v, t) -> {
Optional.ofNullable(v).ifPresent(System.out::println);
Optional.ofNullable(t).ifPresent(Throwable::printStackTrace);
finished.set(true);
});
System.out.println("===========main start no block============");
while (!finished.get()) {
System.out.println(Thread.currentThread().getName());
Thread.sleep(1000);
}
System.out.println("=========main end no block==========");
}
private static double get() {
try {
Thread.sleep(RANDOM.nextInt(10000));
} catch (InterruptedException e) {
e.printStackTrace();
}
return RANDOM.nextDouble();
}
}
Console Output
当把while那段代码注释掉
再次执行
Note: 这个方法启动的是一个daemon线程
<U> CompletableFuture<U> supplyAsync(Supplier<U> supplier, Executor executor)
package org.fool.java8.completable;
import java.util.Optional;
import java.util.Random;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean;
public class CompletableFutureTest3 {
private final static Random RANDOM = new Random(System.currentTimeMillis());
public static void main(String[] args) throws ExecutionException, InterruptedException {
AtomicBoolean finished = new AtomicBoolean(false);
ExecutorService executorService = Executors.newFixedThreadPool(2, r -> {
Thread t = new Thread(r);
t.setDaemon(false);
return t;
});
CompletableFuture.supplyAsync(CompletableFutureTest3::get, executorService).whenComplete((v, t) -> {
Optional.ofNullable(v).ifPresent(System.out::println);
Optional.ofNullable(t).ifPresent(Throwable::printStackTrace);
finished.set(true);
});
System.out.println("===========main start no block============");
while (!finished.get()) {
System.out.println(Thread.currentThread().getName());
Thread.sleep(1000);
}
System.out.println("=========main end no block==========");
}
private static double get() {
try {
Thread.sleep(RANDOM.nextInt(10000));
} catch (InterruptedException e) {
e.printStackTrace();
}
return RANDOM.nextDouble();
}
}
Console Output
Note: 使用ExecutorService默认线程是非Daemon线程,这里将线程显示设置为非Daemon线程,运行完毕,程序还是处于hold状态