我看的網上說thenApplyAsync 是執行在異步執行緒池中的 thenApply 是在同一個執行緒中執行的
我做個了測驗
private static void testThenApply2() {
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
log.info("====future=====");
return 2;
});
future.thenApply(res -> {
log.info("====future=====");
return res + 1;
}).thenApply(res -> {
log.info("====future=====");
return res + 1;
}).thenAccept(res -> System.out.println(res));
CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(() -> {
log.info("====future2=====");
return 2;
});
future2.thenApplyAsync(res -> {
log.info("====future2=====");
return res + 1;
}).thenApplyAsync(res -> {
log.info("====future2=====");
return res + 1;
}).thenAccept(res -> System.out.println(res));
}
其中 log.info 是
public void info(String mes) {
String name = Thread.currentThread().getName();
System.out.println("[ " + name + " ]: " + mes);
}
但是列印出來的結果是
[ ForkJoinPool.commonPool-worker-1 ]: ====future=====
[ ForkJoinPool.commonPool-worker-1 ]: ====future=====
[ ForkJoinPool.commonPool-worker-1 ]: ====future=====
4
[ ForkJoinPool.commonPool-worker-1 ]: ====future2=====
[ ForkJoinPool.commonPool-worker-1 ]: ====future2=====
[ ForkJoinPool.commonPool-worker-1 ]: ====future2=====
4
顯示都是在同一個執行緒中的請問為什么會這樣,求解決
uj5u.com熱心網友回復:
package lambdasinaction.chap11;
import java.util.concurrent.CompletableFuture;
public class TestAsynThreadName {
public static void main(String[] args) {
new TestAsynThreadName().testThenApply2();
}
public void testThenApply2() {
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
info("====future=====");
return 2;
});
future.thenApply(res -> {
info("====future=====");
return res + 1;
}).thenApply(res -> {
info("====future=====");
return res + 1;
}).thenAccept(res -> System.out.println(res));
CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(() -> {
info("====future2=====");
return 2;
});
future2.thenApplyAsync(res -> {
info("====future2=====");
return res + 1;
}).thenApplyAsync(res -> {
info("====future2=====");
return res + 1;
}).thenAccept(res -> System.out.println(res));
}
public void info(String mes) {
String name = Thread.currentThread().getName();
System.out.println("[ " + name + " ]: " + mes);
}
}
<pre>"C:\Program Files\Java\jdk1.8.0_201\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2019.2.3\lib\idea_rt.jar=63037:C:\Program Files\JetBrains\IntelliJ IDEA 2019.2.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_201\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\rt.jar;D:\用戶目錄\下載\Java8inActionSourceCode\Java8InAction-master\target\classes;E:\Repository\junit\junit\4.11\junit-4.11.jar;E:\Repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar" lambdasinaction.chap11.TestAsynThreadName
[ ForkJoinPool.commonPool-worker-1 ]: ====future=====
[ main ]: ====future=====
[ main ]: ====future=====
4
[ ForkJoinPool.commonPool-worker-1 ]: ====future2=====
[ ForkJoinPool.commonPool-worker-1 ]: ====future2=====
[ ForkJoinPool.commonPool-worker-1 ]: ====future2=====
4
Process finished with exit code 0
</pre>
沒有問題哦
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/120090.html
標籤:Java SE
