Commit fda81b5c authored by 刘凯's avatar 刘凯

添加CompletableFutureTest

parent 28674e67
package com.example.demo.concurrent;
import com.fasterxml.jackson.databind.util.JSONPObject;
import org.assertj.core.util.Lists;
import java.sql.Time;
import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
/**
* @author liukai
......@@ -14,30 +22,34 @@ import java.util.concurrent.TimeUnit;
public class CompletableFutureTest {
public static void main(String[] args) {
System.out.println("starttime ---> " + LocalDateTime.now());
Long aa = System.currentTimeMillis();
System.out.println("starttime ---> " + aa);
try {
// Future<Double> future = getPriceAsync("1111");
// Future<Double> future = getPriceAsync2("1111");//
//
Future<Double> future = getPriceAsync3("1111");
Future<Double> futureApply = thenApply("111");
// Future<Double> future = getPriceAsync3("1111");
Future<Double> futurethenCombine = getPriceAsync5("1111");
// Future<Double> futureApply = thenApply("111");
// List<Double> futurethenCombine = listCompletable("1111");//17168(50个处理) 33254(100个处理) 67361 17158 17147
List<Object> futurethenCombine = listCompletable2("1111");//17160 33236 67362 17199 17214
// System.out.println(futurethenCombine.size());
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(System.currentTimeMillis()-aa);
// 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));
Thread.sleep(1000000);
} catch (Exception e) {
e.printStackTrace();
}
......@@ -111,7 +123,7 @@ public class CompletableFutureTest {
private static double calculatePrice(String product) {
delay();
return Math.random() * product.charAt(0) + product.charAt(1);
return 111;
}
......@@ -123,4 +135,36 @@ public class CompletableFutureTest {
}
}
public static List<Double> listCompletable(String product) {
List<Double> o = Lists.newArrayList();
List<String> aa = Lists.newArrayList();
IntStream.range(1,50).forEach(a->aa.add(String.valueOf(a)));
List<CompletableFuture> a1= aa.stream().map(a->CompletableFuture.supplyAsync(() -> calculatePrice(a))).collect(Collectors.toList());
for (int i = 0; i < a1.size(); i++) {
try {
Double re = (Double)a1.get(i).get(2,TimeUnit.SECONDS);
o.add(re);
} catch (Exception e) {
e.printStackTrace();
}
}
// a1.forEach(a->{
// try {
// Double re = (Double)a.get(2,TimeUnit.SECONDS);
// o.add(re);
// } catch (Exception e) {
// e.printStackTrace();
// }
// });
return o;
}
public static List<Object> listCompletable2(String product) {
List<String> aa = Lists.newArrayList();
IntStream.range(1,50).forEach(a->aa.add(String.valueOf(a)));
List<CompletableFuture> a1= aa.stream().map(a->CompletableFuture.supplyAsync(() -> calculatePrice(a))).collect(Collectors.toList());
return a1.stream().map(CompletableFuture::join).collect(Collectors.toList());
}
}
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