差異處

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

連向這個比對檢視

Both sides previous revision 前次修改
下次修改
前次修改
java:java:java8:concurrent:blockingoperationwithcompletablefuture:blocking_inside_cf [2019/01/13 12:12]
tony [How to resolve?]
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>​
行 72: 行 72:
   * 這方法雖然有額外的thread context switch,但處於Blocking狀態的Thread並不會與CPU搶資源,因此工作忙碌時所帶來的效能優勢應可掩蓋此缺點。   * 這方法雖然有額外的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.
  
 ====== ​ ====== ====== ​ ======