Commit 28674e67 authored by 刘凯's avatar 刘凯

添加CompletableFutureTest

parent 41cfce62
package com.example.demo.concurrent;
import java.sql.Time;
import java.time.LocalDateTime;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
/**
* @author liukai
* @Description:
* @date 2018/11/278:06 PM
*/
public class CompletableFutureTest {
public static void main(String[] args) {
System.out.println("starttime ---> " + LocalDateTime.now());
try {
// Future<Double> future = getPriceAsync("1111");
// Future<Double> future = getPriceAsync2("1111");//
//
Future<Double> future = getPriceAsync3("1111");
Future<Double> futureApply = thenApply("111");
Future<Double> futurethenCombine = getPriceAsync5("1111");
System.out.println("over ---> " + LocalDateTime.now());
double aa = future.get(2, TimeUnit.SECONDS);
double bb = futureApply.get(2,TimeUnit.SECONDS);
double cc = futurethenCombine.get(2,TimeUnit.SECONDS);
System.out.println("getPriceAsync3 aa= " + aa);
System.out.println("thenApply bb= " + bb);
System.out.println("futurethenCombine cc= " + cc);
// System.out.println(future.get(3,TimeUnit.SECONDS));
} catch (Exception e) {
e.printStackTrace();
}
}
public static CompletableFuture<Double> thenApply(String product) throws Exception {
return getPriceAsync3(product).thenApply(a -> {
System.out.println(a);
return a + 1;
});
}
public static Future<Double> getPriceAsync(String product) {
CompletableFuture<Double> futurePrice = new CompletableFuture<>();
new Thread(() -> {
double price = calculatePrice(product);
int a = 1 / 0;
futurePrice.complete(price);
}).start();
return futurePrice;
}
public static Future<Double> getPriceAsync2(String product) {
CompletableFuture<Double> futurePrice = new CompletableFuture<>();
new Thread(() -> {
try {
double price = calculatePrice(product);
int a = 1 / 0;
futurePrice.complete(price);
} catch (Exception e) {
futurePrice.completeExceptionally(e);
}
}).start();
return futurePrice;
}
/**
* @author 工厂方法
*/
public static CompletableFuture<Double> getPriceAsync3(String product) {
CompletableFuture<Double> futurePrice = CompletableFuture.supplyAsync(() -> calculatePrice(product));
return futurePrice;
}
public static Future<Double> getPriceAsync4(String product) {
CompletableFuture<Double> futurePrice = new CompletableFuture<>();
new Thread(() -> {
try {
double price = calculatePrice(product);
futurePrice.complete(price);
} catch (Exception e) {
futurePrice.completeExceptionally(e);
}
}).start();
return futurePrice;
}
public static Future<Double> getPriceAsync5(String product) {
return CompletableFuture.supplyAsync(() -> calculatePrice(product)).thenCombine( CompletableFuture.supplyAsync(() -> calculatePrice(product)),(a,b)-> {
System.out.println("getPriceAsync5 a = "+a);
System.out.println("getPriceAsync5 b = "+b);
return a+b;
});
}
private static double calculatePrice(String product) {
delay();
return Math.random() * product.charAt(0) + product.charAt(1);
}
public static void delay() {
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
......@@ -5,6 +5,7 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
public class UseFuture implements Callable<String>{
private String para;
......@@ -48,7 +49,7 @@ public class UseFuture implements Callable<String>{
e.printStackTrace();
}
//调用获取数据方法,如果call()方法没有执行完成,则依然会进行等待
System.out.println("数据:" + future.get());
System.out.println("数据:" + future.get(1,TimeUnit.SECONDS));
System.out.println("数据:" + future2.get());
executor.shutdown();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment