File tree Expand file tree Collapse file tree 1 file changed +8
-3
lines changed
03concurrency/0301/src/main/java/java0/conc0303 Expand file tree Collapse file tree 1 file changed +8
-3
lines changed Original file line number Diff line number Diff line change 11package java0 .conc0303 ;
22
3- import java .util .concurrent .CompletableFuture ;
3+ import java .util .concurrent .atomic . AtomicReference ;
44
55/**
66 * 本周作业:(必做)思考有多少种方式,在main函数启动一个新线程或线程池,
@@ -27,12 +27,17 @@ public static void main(String[] args) throws Exception {
2727 // });
2828 // executorService.shutdown();
2929 // countDownLatch.await();
30- int result = CompletableFuture .supplyAsync (()-> sum ()).join ();
30+ // int result = CompletableFuture.supplyAsync(()-> sum()).join();
3131
3232 //int result = sum(); //这是得到的返回值
3333 //int result = future.get();
34+ AtomicReference <Integer > result = new AtomicReference <>();
35+ new Thread (() -> result .set (sum ())).start ();
3436 // 确保 拿到result 并输出
35- System .out .println ("异步计算结果为:" +result );
37+ while (result .get () == null ) {
38+
39+ }
40+ System .out .println ("异步计算结果为:" +result .get ());
3641
3742 System .out .println ("使用时间:" + (System .currentTimeMillis () - start ) + " ms" );
3843
You can’t perform that action at this time.
0 commit comments