日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

FutureTask isDone 返回 false

發(fā)布時(shí)間:2023/12/10 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 FutureTask isDone 返回 false 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

大家好,我是烤鴨:

? ????今天看一下 FutureTask源碼。好吧,其實(shí)遇到問題了,哪里不會(huì)點(diǎn)哪里。

偽代碼

package src.executor;import org.springframework.scheduling.annotation.AsyncResult; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;import java.util.concurrent.*;/***@program: *@description: 測試*@author: *@email: *@create: 2021/07/07 11:35*/ public class FutureAndLatchTest {static ThreadPoolTaskExecutor initTaskPool(){ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();taskExecutor.setCorePoolSize(15);taskExecutor.setMaxPoolSize(60);taskExecutor.setQueueCapacity(200);taskExecutor.setKeepAliveSeconds(60);taskExecutor.setThreadNamePrefix("test-");taskExecutor.setWaitForTasksToCompleteOnShutdown(true);taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());taskExecutor.setAwaitTerminationSeconds(60);taskExecutor.initialize();return taskExecutor;}public static void main(String[] args) {ThreadPoolTaskExecutor taskPool = initTaskPool();for (int i = 0; i < 20; i++) {CountDownLatch latch = new CountDownLatch(2);Future<Integer> f1 = taskPool.submit(() ->future(latch));Future<Integer> f2 = taskPool.submit(() ->future(latch));try {latch.await(200, TimeUnit.MILLISECONDS);} catch (InterruptedException e) {e.printStackTrace();}if(!f1.isDone()){System.out.println("f1 is not done");}if(!f2.isDone()){System.out.println("f2 is not done");}}System.out.println("taskPool finish");}private static Integer future(CountDownLatch latch) {try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}finally {latch.countDown();}return 1;} }

這段代碼維護(hù)了一個(gè)線程池,執(zhí)行兩個(gè)線程用CountDownLatch做超時(shí)控制,再判斷線程是否完成,這段代碼會(huì)輸出 is not done么??聪聦?shí)際結(jié)果。(有概率復(fù)現(xiàn))

輸出: f1 is not done f1 is not done f2 is not done f1 is not done f1 is not done f2 is not done f1 is not done taskPool finish

原因分析

看一下 Future 的方法注釋,就是方法是否執(zhí)行完成,理論上沒問題啊。

/*** Returns {@code true} if this task completed.** Completion may be due to normal termination, an exception, or* cancellation -- in all of these cases, this method will return* {@code true}.** @return {@code true} if this task completed*/ boolean isDone();

而實(shí)現(xiàn)調(diào)用的 FutureTask

public boolean isDone() {return state != NEW; }

出現(xiàn)這個(gè)state還得再看下源碼,state用來維護(hù)線程狀態(tài)的,注釋也說明了幾種狀態(tài)的流轉(zhuǎn)。

/*** The run state of this task, initially NEW. The run state* transitions to a terminal state only in methods set,* setException, and cancel. During completion, state may take on* transient values of COMPLETING (while outcome is being set) or* INTERRUPTING (only while interrupting the runner to satisfy a* cancel(true)). Transitions from these intermediate to final* states use cheaper ordered/lazy writes because values are unique* and cannot be further modified.** Possible state transitions:* NEW -> COMPLETING -> NORMAL* NEW -> COMPLETING -> EXCEPTIONAL* NEW -> CANCELLED* NEW -> INTERRUPTING -> INTERRUPTED*/ private volatile int state; private static final int NEW = 0; private static final int COMPLETING = 1; private static final int NORMAL = 2; private static final int EXCEPTIONAL = 3; private static final int CANCELLED = 4; private static final int INTERRUPTING = 5; private static final int INTERRUPTED = 6;

新建 -> 進(jìn)行中 -> 完成、新建 -> 進(jìn)行中 -> 異常、新建 -> 取消、新建 ->等待 ->取消

更多詳細(xì)的可以看下這篇文章。

https://blog.csdn.net/qq_35067322/article/details/104872102

看下2012年的這個(gè)提問吧,和有位大神的回復(fù)。

https://stackoverflow.com/questions/9604713/future-isdone-returns-false-even-if-the-task-is-done

簡單來說,就是子線程里調(diào)用finally 執(zhí)行 countdownlatch.countdown()的時(shí)候,主線程發(fā)現(xiàn) latch 變成0了就繼續(xù)執(zhí)行,但是這個(gè)時(shí)候 futureTask還在finally里,state沒變過來。就是毫秒級(jí)別的線程切換,主線程在那一瞬間優(yōu)先執(zhí)行。

優(yōu)化

原來代碼里是想監(jiān)聽多線程的執(zhí)行結(jié)果,執(zhí)行完成后再去執(zhí)行其他的操作。怎么樣才能監(jiān)聽到實(shí)際結(jié)果呢,改為 Future.get();

try {f1.get(5,TimeUnit.MILLISECONDS);f2.get(5,TimeUnit.MILLISECONDS); } catch (Exception e) {e.printStackTrace(); }

get 方法為啥沒問題呢,看下源碼。

/*** @throws CancellationException {@inheritDoc}*/ public V get(long timeout, TimeUnit unit)throws InterruptedException, ExecutionException, TimeoutException {if (unit == null)throw new NullPointerException();int s = state;if (s <= COMPLETING &&(s = awaitDone(true, unit.toNanos(timeout))) <= COMPLETING)throw new TimeoutException();return report(s); }

狀態(tài)沒完成,就等他完成就好了。妥妥的CAS 樂觀鎖實(shí)現(xiàn)。

/*** Awaits completion or aborts on interrupt or timeout.** @param timed true if use timed waits* @param nanos time to wait, if timed* @return state upon completion*/ private int awaitDone(boolean timed, long nanos)throws InterruptedException {final long deadline = timed ? System.nanoTime() + nanos : 0L;WaitNode q = null;boolean queued = false;for (;;) {if (Thread.interrupted()) {removeWaiter(q);throw new InterruptedException();}int s = state;if (s > COMPLETING) {if (q != null)q.thread = null;return s;}else if (s == COMPLETING) // cannot time out yetThread.yield();else if (q == null)q = new WaitNode();else if (!queued)queued = UNSAFE.compareAndSwapObject(this, waitersOffset,q.next = waiters, q);else if (timed) {nanos = deadline - System.nanoTime();if (nanos <= 0L) {removeWaiter(q);return state;}LockSupport.parkNanos(this, nanos);}elseLockSupport.park(this);} }

屬實(shí)是有點(diǎn)水了…

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

總結(jié)

以上是生活随笔為你收集整理的FutureTask isDone 返回 false的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。