差異處

這裏顯示兩個版本的差異處。

連向這個比對檢視

下次修改
前次修改
java:java:java8:concurrent:blockingoperationwithcompletablefuture:blocking_inside_cf [2019/01/13 12:06]
tony 建立
java:java:java8:concurrent:blockingoperationwithcompletablefuture:blocking_inside_cf [2023/06/25 09:48] (目前版本)
行 39: 行 39:
  
 @Test @Test
-public void testSendSync(){ +public void testSendAsync(){ 
- CompletableFuture<​Response> ​sendSync ​= CompletableFuture.supplyAsync(()->​{+ CompletableFuture<​Response> ​sendAsync ​= CompletableFuture.supplyAsync(()->​{
  dumpCurrentThreadName("​supplyAsync"​);​  dumpCurrentThreadName("​supplyAsync"​);​
  return launchTaskWithAuxThread(()->​new BlockingJob().invoke());​  return launchTaskWithAuxThread(()->​new BlockingJob().invoke());​
行 53: 行 53:
  }, es);  }, es);
   
- sendSync.join();+ sendAsync.join();
 } }
 </​code>​ </​code>​
行 70: 行 70:
   * 如果此ExecutorService專門用於CPU Bound工作,這做法可分離出Blocking操作避免影響到ThreadPool的生產力。   * 如果此ExecutorService專門用於CPU Bound工作,這做法可分離出Blocking操作避免影響到ThreadPool的生產力。
   * 範例中針對Blocking操作是建立單一Thread的ExecutorService,但如果會有IO最大限制的情況下,這部分需要控管數量。   * 範例中針對Blocking操作是建立單一Thread的ExecutorService,但如果會有IO最大限制的情況下,這部分需要控管數量。
-  * 這方法雖然有額外的thread context switch,但工作忙碌時所帶來的效能優勢應可掩蓋此缺點。+  * 這方法雖然有額外的thread context switch,但處於Blocking狀態的Thread並不會與CPU搶資源,因此工作忙碌時所帶來的效能優勢應可掩蓋此缺點。
  
-Note. 我的範例程式只是實現的方法之一,應要根據需求做調整。 例如原始投影片中,是透過responseReceived.complete讓工作回到compose之後繼續往下處理。+Note. 我的範例程式只是實現的方法之一,應要根據需求做調整。 例如原始投影片中,是透過responseReceived.completeAsync讓工作回到compose之後繼續往下處理。
 ===== Reference ===== ===== Reference =====
-  * [[https://​qconsf.com/​sf2017/​system/​files/​presentation-slides/​cf.pdf|Asynchronous API with +  * [[https://​qconsf.com/​sf2017/​system/​files/​presentation-slides/​cf.pdf|Asynchronous API with CompletableFuture - Performance Tips and Tricks]] 
-CompletableFuture - Performance Tips and Tricks]]+  * Java Concurrency In Practice, 11.6. Reducing Context Switch overhead.
  
 ====== ​ ====== ====== ​ ======