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

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 Executor 的 JavaFX 應(yīng)用程序在退出時掛起

使用 Executor 的 JavaFX 應(yīng)用程序在退出時掛起

天涯盡頭無女友 2022-01-06 18:50:33
我正在嘗試克隆 Android 的 AsyncTask 以在 JavaFX 應(yīng)用程序中使用。這是我想出的代碼:import java.util.concurrent.Executor;import java.util.concurrent.Executors;abstract public class AsyncTask<Param, Result>{    private Param param;    private static Executor executor;    public AsyncTask()    {        if (executor == null)            executor = Executors.newSingleThreadExecutor();    }    protected void onPreExecute()    {    }    protected Result doInBackground(Param param)    {        return null;    }    protected void onPostExecute(Result result)    {    }    final public void execute(Param param)    {        this.param = param;        onPreExecute();        Task t = new Task();        executor.execute(t);    }    private class Task implements Runnable    {        public void run()        {            Result result = doInBackground(param);            onPostExecute(result);        }    }}我可以在我的 JavaFX 應(yīng)用程序中使用這個類,它工作正常,除了一件事:當(dāng)我關(guān)閉主窗口時,JVM 掛起而不是干凈地退出。我必須強制退出該應(yīng)用程序。我認(rèn)為問題與 Executor 有關(guān)。因為我沒有發(fā)出shutdown(),Executor 掛起等待更多任務(wù)執(zhí)行。由于 AsyncTask 是 Java Executor 的包裝器,因此擴展 AsyncTask 的類將無法直接訪問 Executor,因此無法發(fā)出 shutdown()。如何有序關(guān)閉 Executor?
查看完整描述

1 回答

?
POPMUISE

TA貢獻1765條經(jīng)驗 獲得超5個贊

您要么需要從Application.stop方法中關(guān)閉執(zhí)行程序,要么確保Executor的線程不會通過使用守護線程來阻止 JVM 關(guān)閉:


if (executor == null) {

    executor = Executors.newSingleThreadExecutor(r -> {

        Thread t = new Thread(r);

        t.setDaemon(true);

        return t;

    });

}


查看完整回答
反對 回復(fù) 2022-01-06
  • 1 回答
  • 0 關(guān)注
  • 135 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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