java非同步之CompletableFuture
- 2019 年 10 月 5 日
- 筆記
非同步一般用來處理耗時非常多的計算,如果你的計算量不是很大,調用非同步方法反而沒有執行來的快,我在這裡為大家簡單的整理一下非同步的知識以及用法,我寫了一個Main的類,大家可以跑其中的一個方法,把其他的注釋掉,這樣就可以對非同步有一個大致的了解了。
方法 |
入參 |
返回值 |
---|---|---|
runAsync |
Runnable |
Void |
thenAccept |
T |
void |
thenApply |
T |
U |
thenRun |
Runnable |
Void |
supplyAsync |
T |
U |
/** * main * * @author [email protected] |[email protected] |gfu * @date 2019/8/30 */ public class Main { public static void main(String[] args) throws InterruptedException { Async async = new Async(); async.doAsync0(); async.doAsync1(); async.doAsync2(); async.doAsync3(); async.doAsync4(); Thread.sleep(6000); } }
import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; /** * async * * @author [email protected] |[email protected] |gfu * @date 2019/8/30 */ public class Async { static final int NINE_MULTI_BY_NINE_LENGTH = 10; public long print() { System.out.println(Thread.currentThread().getName() + Thread.currentThread().getId() + ":"); long start0 = System.nanoTime(); System.out.println("----------start---------"); for (int i = 0; i < NINE_MULTI_BY_NINE_LENGTH; i++) { for (int j = 0; j < NINE_MULTI_BY_NINE_LENGTH; j++) { System.out.print(i * j + " "); } System.out.println(); } long end0 = System.nanoTime(); System.out.println(Thread.currentThread().getName() + Thread.currentThread().getId() + ":" + (end0 - start0)); return end0 - start0; } public void doAsync0() { CompletableFuture.runAsync(this::print); } /** * need return value */ public void doAsync1() { CompletableFuture.supplyAsync(() -> print()); } /** * you can see it's method to judge whether must need params or return * thenAccept can accept pre-stage's result as params * thenAccept and thenApply must need params * thenAccept can return void but thenApply need to have a return value */ public void doAsync2() { CompletableFuture.supplyAsync(() -> print()) .thenAccept((i) -> { System.out.println("-----------------------------------------------"); System.out.println(Thread.currentThread().getName() + Thread.currentThread().getId() + "#" + i); }); } /** * complete and CompletableFuture's get method can let you get it.s async calculate result */ public void doAsync3() { CompletableFuture cf = new CompletableFuture(); cf.complete("hello"); try { System.out.println(cf.get()); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } } public void doAsync4() { CompletableFuture cf0 = CompletableFuture.supplyAsync(() -> print()); CompletableFuture cf1 = CompletableFuture.supplyAsync(() -> "world"); CompletableFuture cf2 = CompletableFuture.supplyAsync(() -> "java8"); CompletableFuture cf = CompletableFuture.allOf(cf0); try { System.out.println(Thread.currentThread().getName() + Thread.currentThread().getId() + ":" + cf0.get()); System.out.println(Thread.currentThread().getName() + Thread.currentThread().getId() + ":" + cf1.get()); System.out.println(Thread.currentThread().getName() + Thread.currentThread().getId() + ":" + cf2.get()); System.out.println(cf.get()); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } } }