比较金典的例子:
import lombok.AllArgsConstructor; import lombok.Data; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.experimental.Accessors; import java.util.Arrays; import java.util.List; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; public class CompletableFutureMallDemo { static List<NetMall> list= Arrays.asList( new NetMall("jd"), new NetMall("dangdang"), new NetMall("taobao") ); public static List<String> getPrice(List<NetMall> list,String productName){ return list.stream().map( netMall -> String.format(productName + " in %s price is %.2f", netMall.getNetMallName(), netMall.calcPrice(productName))) .collect(Collectors.toList()); } public static List<String> getPriceByCompletableFuture(List<NetMall> list,String productName){ return list.stream().map(netMall -> CompletableFuture.supplyAsync(() -> String.format(productName + " in %s price is %.2f", netMall.getNetMallName(), netMall.calcPrice(productName)) )).collect(Collectors.toList()) .stream().map(s -> s.join()).collect(Collectors.toList()); } public static void main(String[] args) { long starTime=System.currentTimeMillis(); List<String> list1= getPrice(list,"mysql"); for (String element:list1){ System.out.println(element); } long endTime=System.currentTimeMillis(); System.out.println("-------------costTime:"+(endTime-starTime)+"毫秒"); System.out.println("-----------------"); long starTime1=System.currentTimeMillis(); List<String> list2= getPriceByCompletableFuture(list,"mysql"); for (String element:list2){ System.out.println(element); } long endTime1=System.currentTimeMillis(); System.out.println("-------------costTime:"+(endTime1-starTime1)+"毫秒"); } } class NetMall{ @Getter private String netMallName; public NetMall(String netMallName) { this.netMallName = netMallName; } public double calcPrice(String productName){ try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } return ThreadLocalRandom.current().nextDouble()*2+productName.charAt(0); } } @AllArgsConstructor @NoArgsConstructor @Data @Accessors(chain = true) class Student{ private Integer id; private String studentName; private String major; }