模拟在线商店功能:
1.Shop.java
package com.asyn;
import java.util.concurrent.CompletableFuture;import java.util.concurrent.Future; public class Shop {
private double price;
private String Name; public double getPrice() {
return price;
} public void setPrice(double price) {
this.price = price;
} public Shop(String name) {
super();
Name = name;
} public String getName() {
return Name;
} public void setName(String name) {
Name = name;
} public double getPrice(String product) {
return calculatePrice(product);
} public Future<Double> getPriceAsyn(String product) {
CompletableFuture<Double> futurePrice = new CompletableFuture<>();
new Thread(() -> {
try {
double price = calculatePrice(product);
futurePrice.complete(price);
} catch (Exception ex) {
//获取多线程的异常的操作
futurePrice.completeExceptionally(ex);
}
}).start();
return futurePrice;
}
// 工厂模式创建多线程
public Future<Double> getPriceAsync(String product) {
return CompletableFuture.supplyAsync(() -> calculatePrice(product));
} private 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);
}
} }
2.ShopAsyn.java
package com.asyn;
import java.util.concurrent.Future;
public class ShopAsyn {
public static void main(String[] args) {
Shop shop = new Shop("BestShop");
long start = System.nanoTime();
Future<Double> futurePrice = shop.getPriceAsyn("my favorite product");
//工厂模式生成
Future<Double> futurePrice2 = shop.getPriceAsync("my favorite product");
long invocationTime = ((System.nanoTime() - start) / 1_000_000);
System.out.println("Invocation returned after " + invocationTime + " msecs");
System.out.println("doSomething else");
System.out.println("doSomething else");
System.out.println("doSomething else");
System.out.println("doSomething else");
System.out.println("doSomething else");
System.out.println("doSomething else");
try {
double price = futurePrice.get();
System.out.printf("Price is %.2f%n", price);
double price2 = futurePrice2.get();
System.out.printf("Price is %.2f%n", price2);
} catch (Exception e) {
throw new RuntimeException(e);
}
long retrievalTime = ((System.nanoTime() - start) / 1_000_000);
System.out.println("Price returned after " + retrievalTime + " msecs");
}
}
输出结果:
Invocation returned after 38 msecs
doSomething else
doSomething else
doSomething else
doSomething else
doSomething else
doSomething else
Price is 168.81
Price returned after 1061 ms
ecs
说明:
再多加几个“dosomething else ” 处理也是1061ms ,也就是说,只要这个插入输出的方法耗时小于1s, 返回的耗时都是1061.
尽管超出了1s, 返回的耗时也是比阻塞式的请求耗时少。
更好的解决方案:
3种解决方案速度比较:
package com.asyn;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
public class AnotherAsync {static List<Shop> shops = Arrays.asList(new Shop("BestPrice"), new Shop("LetsSaveBig"), new Shop("MyFavoriteShop"),
new Shop("BuyItAll")); public static void main(String... strings) {
new Thread(() -> {
long start = System.nanoTime();
System.out.println(findPrices("myPhone27S"));
long duration = (System.nanoTime() - start) / 1_000_000;
System.out.println("1Done in " + duration + " msecs");
System.out.println("1------------------------------");
}).start(); new Thread(() -> {
long start1 = System.nanoTime();
System.out.println(findPrices2("myPhone27S"));
long duration1 = (System.nanoTime() - start1) / 1_000_000;
System.out.println("2Done in " + duration1 + " msecs");
System.out.println("2------------------------------"); }).start();
new Thread(() -> {
long start2 = System.nanoTime();
System.out.println(findPrices3("myPhone27S"));
long duration2 = (System.nanoTime() - start2) / 1_000_000;
System.out.println("3Done in " + duration2 + " msecs");
System.out.println("3------------------------------");
}).start();
} // 耗时4s+
private static List<String> findPrices(String product) {
return shops.stream().map(shop -> String.format("%s price is %.2f", shop.getName(), shop.getPrice(product)))
.collect(Collectors.toList());
}
// 利用ParallelStream
// 耗时1065ms
public static List<String> findPrices2(String product) {
return shops.parallelStream()
.map(shop -> String.format("%s price is %.2f", shop.getName(), shop.getPrice(product)))
.collect(Collectors.toList());
} // 工厂模式查找
public static List<String> findPrices3(String product) {
// 利用工厂方法进行查找
List<CompletableFuture<String>> priceFutures = shops.stream()
.map(shop -> CompletableFuture
.supplyAsync(() -> String.format("%s price is %.2f", shop.getName(), shop.getPrice())))
.collect(Collectors.toList());
return priceFutures.stream().map(CompletableFuture::join).collect(Collectors.toList());
} }
结果:
[BestPrice price is 0.00, LetsSaveBig price is 0.00, MyFavoriteShop price is 0.00, BuyItAll price is 0.00]
3Done in 1010 msecs
3------------------------------
[BestPrice price is 215.30, LetsSaveBig price is 191.24, MyFavoriteShop price is 206.11, BuyItAll price is 190.68]
2Done in 1030 msecs
2------------------------------
[BestPrice price is 223.95, LetsSaveBig price is 211.89, MyFavoriteShop price is 216.61, BuyItAll price is 173.76]
1Done in 4009 msecs
1------------------------------