私がオーダー関連の統計インターフェースを持っている場合は、サンプルデータは、3を返却する必要があります。注文数今日、今日の売上高、総売上高を。
私たちの一般的なアプローチは、3つのシリアル関数を呼び出すことで、それぞれの呼び出しは、合計時間を1秒を要する場合は、これら三つのシリアル実行時に呼び出すと、呼び出し元に呼び出しから返された結果は、3つの呼び出しです3秒。
それは3間のコールの順序は重要ではありませんので、並列実行効率の使用が良くなるので、このアプローチは、非常に非効率的です。たとえば、スレッドプールExecutorServiceの非同期呼び出しを使用して。
実際にはJava8は非常に高速なハードウェアCompletableFutureクラスを提供し、あなたはまた、非同期技術を実装することができます:
1 インポートlombok.extern.slf4j.Slf4j。 2 インポートorg.springframework.stereotype.Service。 3 4 インポートjava.util.concurrent.CompletableFuture。 5 輸入java.util.concurrent.TimeUnit。 6 7 SLF4J @ 8 @Service 9 パブリック クラスOrderServiceの{ 10 11 / ** 12 *今日订单数 13 * 14 * @return 15 * / 16 公共 CompletableFutureの<string> todayOrderCount(){ 17 返す CompletableFuture.supplyAsync(() - > この.getTodayOrderCount()); 18 } 19 20 公衆 CompletableFutureの<string> todayTurnover(){ 21 リターン CompletableFuture.supplyAsync(() - > この.getTodayTurnover())。 22 } 23 24 公衆 CompletableFutureの<string> totalTurnover(){ 25 リターン CompletableFuture.supplyAsync(() - > この.getTotalTurnover())。 26 } 27 28 プライベート文字列getTodayOrderCount(){ 29 System.out.println( ">>>>>>>クエリ注文今日:" + 。にThread.currentThread()のgetName()); 30 のtry { 31である (1 TimeUnit.SECONDS.sleep ); 32 } キャッチ(InterruptedExceptionありますE){ 33は e.printStackTrace(); 34である } 35 リターン "50" ; 36 } 37 38である / ** 39 *日の売上高 40 * 41である * @return 42である * / 43である 専用ストリングgetTodayTurnoverは(){ 44れます System.out.println( ">>>>>>>クエリトランザクション今日:" + 。にThread.currentThread()のgetName()); 45 のtry { 46である TimeUnit.SECONDS.sleep(1 ); 47 } キャッチ(InterruptedExceptionありますE){ 48 e.printStackTrace(); 49 } 50 リターン "200は" ; 51である } 52は 53である / ** 54れる *総売上高 55 * 56である * @return 57である * / 58 プライベートストリングgetTotalTurnover(){ 59 System.out.println( "総売上高クエリを>>>>>>>:" + 。にThread.currentThread()のgetName()); 60 のtry { 61である (1 TimeUnit.SECONDS.sleep ); 62れる } キャッチ(InterruptedExceptionありますE){ 63は e.printStackTrace(); 64 } 65 リターン "800" ; 66 } 67 }
1つの インポートcom.alibaba.fastjson.JSONObject。 2 インポートcom.example.sb.service.test.impl.OrderService。 3 輸入lombok.extern.slf4j.Slf4j。 4 インポートorg.springframework.beans.factory.annotation.Autowired。 5 輸入org.springframework.web.bind.annotation.GetMapping。 6 インポートorg.springframework.web.bind.annotation.RequestMapping。 7 インポートorg.springframework.web.bind.annotation.RestController。 8 9 インポートjava.util.concurrent.CompletableFuture。 10 11 SLF4J @ 12 @RestController 13 @RequestMapping( "/注文" ) 14 パブリック クラスOrderController { 15 16 @Autowired 17 プライベートOrderServiceのOrderServiceの。 18 19 @GetMapping( "/レポート" ) 20 公衆JSONObjectレポート(){ 21 長い開始= にSystem.currentTimeMillis(); 22 JSONObject JSON = orderReport()。 23 のSystem.out.println( "耗时:" +(のSystem.currentTimeMillis() - )を開始)。 24 リターンJSON。 25 } 26 27 プライベートJSONObject orderReport(){ 28 CompletableFutureの<string> todayOrderCountFuture = orderService.todayOrderCount()。 29 CompletableFutureの<string> todayTurnoverFuture = orderService.todayTurnover()。 30 CompletableFutureの<string> totalTurnoverFuture = orderService.totalTurnover()。 31 32 JSONObject JSON = 新しいJSONObject()。 33 34 todayOrderCountFuture.whenComplete((V、T) - > { 35 json.put( "todayOrderCountFuture" 、V); 36 })。 37 todayTurnoverFuture.whenComplete((V、T) - > { 38 json.put( "todayTurnoverFuture" 、V); 39 })。 40 totalTurnoverFuture.whenComplete((V、T) - > { 41 json.put( "totalTurnoverFuture" 、V); 42 })。 43 44 CompletableFuture.allOf(todayOrderCountFuture、todayTurnoverFuture、totalTurnoverFuture) 45 .thenRun(() - >のSystem.out.println( "完成!!!!" )) 46 .join()。 47 リターンJSON。 48 } 49 }
ブラウザアクセス:HTTP:// localhostを:8080 /発注/レポートの 実行結果は次の通りのショットは、次のとおりです。
コールがシミュレートされているのでOrderServiceのそれぞれは、パラレル、時間のかかる自然の中3つのコールが最終的に1秒、1秒を要しました。