第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何識(shí)別已取消的 ScheduledFuture 是否實(shí)際上未取消?

如何識(shí)別已取消的 ScheduledFuture 是否實(shí)際上未取消?

紅糖糍粑 2022-11-02 10:29:40
我正在使用 ScheduledExecutorService 并提交這樣的任務(wù):future = scheduledExecutorService.schedule(myRunnableTask, delay, timeunit)然而,某個(gè)事件可能會(huì)在不確定的時(shí)間后發(fā)生,這表明不再需要此任務(wù)。所以我需要取消這個(gè)任務(wù),我正在使用boolean cancelled = future.cancel(false)線。取消后,我必須根據(jù)提交的可運(yùn)行文件是否實(shí)際運(yùn)行來(lái)采取不同的操作。在這里,讓我們首先進(jìn)入 Oracle 文檔并閱讀cancelled標(biāo)志的含義:https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Future.html#cancel(boolean)返回:如果任務(wù)無(wú)法取消,則返回 false,通常是因?yàn)樗呀?jīng)正常完成;否則為真這就是關(guān)于返回值的全部?jī)?nèi)容。似乎寫(xiě)這個(gè)文本行的人不確定false這里的返回值,但我想我可以接受?,F(xiàn)在讓我們專注于案例,當(dāng)它返回時(shí)true。這里有兩種可能:該任務(wù)實(shí)際上已被取消并且 runnable 從未運(yùn)行過(guò)。可運(yùn)行對(duì)象正在運(yùn)行,因此無(wú)法取消。(除非我做了一些線程中斷邏輯,我真的不想這樣做)我對(duì)這兩種情況都沒(méi)有意見(jiàn),但我想知道實(shí)際發(fā)生了哪一種情況并采取相應(yīng)的行動(dòng)。如果runnable正在處理中,那么我可以接受它完成它的工作,我想等待它完成然后做一件事。但如果它被取消并且根本不會(huì)運(yùn)行,我想做另一件事。你能推薦一種方法嗎?我錯(cuò)過(guò)了什么嗎?
查看完整描述

2 回答

?
料青山看我應(yīng)如是

TA貢獻(xiàn)1772條經(jīng)驗(yàn) 獲得超8個(gè)贊

您可以通過(guò)以下方式實(shí)現(xiàn)您的目標(biāo):

  • ASet<Runnable>跟蹤Runnable線程池已開(kāi)始執(zhí)行的 s。

  • AMap<ScheduledFuture<?>, Runnable>將 a 映射ScheduledFuture<?>到其各自的Runnable.

    • 安排任務(wù)后,您應(yīng)立即將ScheduledFuture及其各自添加RunnableMap.

    • 如果在調(diào)度任務(wù)本身的情況下以原子方式執(zhí)行插入Map,那么您可以避免即使在取消之后也ScheduledFuture從未添加到的邊緣情況。Map

我建議將您更改ScheduledExecutorService為 a ScheduledThreadPoolExecutor,這將允許您覆蓋其beforeExecute(Thread, Runnable)方法;這個(gè)方法在任務(wù)被池運(yùn)行之前立即調(diào)用,在它已經(jīng)被分配了一個(gè)將執(zhí)行任務(wù)的線程之后。

覆蓋此方法時(shí),您可以RunnableSet<Runnable>.

然后,當(dāng) aScheduledFuture被取消時(shí),您可以調(diào)用set.contains(map.get(future)),它會(huì)告訴您RunnableScheduledFuture映射到的)是否已執(zhí)行。


請(qǐng)注意,您的Set<Runnable>Map<ScheduledFuture<?>, Runnable>實(shí)現(xiàn)可能必須是線程安全的,以避免可能的競(jìng)爭(zhēng)條件。


查看完整回答
反對(duì) 回復(fù) 2022-11-02
?
千萬(wàn)里不及你

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超9個(gè)贊

我最終為這個(gè)問(wèn)題寫(xiě)了這樣的東西。源代碼和一些單元測(cè)試可以在https://github.com/nuzayats/cancellabletaskexecutor找到


public class CancellableTaskExecutor {


    private final ScheduledExecutorService es;

    private final Logger log;


    /**

     * For a unit test to replicate a particular timing

     */

    private final Runnable hookBetweenCancels;


    public CancellableTaskExecutor(ScheduledExecutorService es, Logger log) {

        this(es, log, () -> {

            // nop

        });

    }


    // For unit tests

    CancellableTaskExecutor(ScheduledExecutorService es, Logger log, Runnable hookBetweenCancels) {

        this.es = es;

        this.log = log;

        this.hookBetweenCancels = hookBetweenCancels;

    }


    public Execution schedule(Runnable task, long delay, TimeUnit unit) {

        CancellableRunnable runnable = new CancellableRunnable(task);

        ScheduledFuture<?> future = es.schedule(runnable, delay, unit);

        return new Execution(future, runnable);

    }


    public class Execution {


        private final ScheduledFuture<?> future;

        private final CancellableRunnable runnable;


        private Execution(ScheduledFuture<?> future, CancellableRunnable runnable) {

            this.future = future;

            this.runnable = runnable;

        }


        /**

         * @return true when the task has been successfully cancelled and it's guaranteed that

         * the task won't get executed. otherwise false

         */

        public boolean cancel() {

            boolean cancelled = runnable.cancel();

            hookBetweenCancels.run();


            // the return value of this call is unreliable; see https://stackoverflow.com/q/55922874/3591946

            future.cancel(false);


            return cancelled;

        }

    }


    private class CancellableRunnable implements Runnable {


        private final AtomicBoolean cancelledOrStarted = new AtomicBoolean();

        private final Runnable task;


        private CancellableRunnable(Runnable task) {

            this.task = task;

        }


        @Override

        public void run() {

            if (!cancelledOrStarted.compareAndSet(false, true)) {

                return; // cancelled, forget about the task

            }

            try {

                task.run();

            } catch (Throwable e) {

                log.log(Level.WARNING, "Uncaught Exception", e);

            }

        }


        boolean cancel() {

            return cancelledOrStarted.compareAndSet(false, true);

        }

    }

}



查看完整回答
反對(duì) 回復(fù) 2022-11-02
  • 2 回答
  • 0 關(guān)注
  • 107 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)