gpt4 book ai didi

java - 我的 ScheduledThreadPoolExecutor 没有关闭

转载 作者:行者123 更新时间:2023-12-01 10:35:25 26 4
gpt4 key购买 nike

我有一个带有 ScheduledThreadPoolExecutor 的 Android 库。它每两秒执行一次任务来检查条件,并在应用程序调用库的enable()函数时启动。当应用程序调用disable()函数时,该函数会取消ScheduledFuture任务并在执行器上调用shutdownNow()。问题是它没有停止。在应用程序的日志中,我可以看到禁用()被调用,并查看到达 Exec Shutdown 的位置,但我仍然看到 Exec Runnable 消息在后台运行。事实上,它会一直运行,直到我杀死该应用程序。有什么想法为什么任务没有停止吗?相关代码如下:

private ScheduledThreadPoolExecutor exec;
private ScheduledFuture<?> sf;


private void enable(){
exec = new ScheduledThreadPoolExecutor(1);
long period = 2000;
exec.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
exec.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
sf = exec.scheduleAtFixedRate(new SwitchCheck(), 0, period, TimeUnit.MILLISECONDS);
}


private void disable(){
if(exec != null) {
try {
Log.d(LOG_TAG,"Exec Shutdown");
sf.cancel(true);
exec.shutdownNow();
exec = null;
} catch (Exception e){
e.printStackTrace();
}
}
}

class SwitchCheck implements Runnable {

@Override
public void run() {
Log.e(LOG_TAG, "***Exec Runnable***");

}
}

这是相关的logcat。 updateVisibility 被调用的地方就是我离开 Activity 的地方。

01-13 10:28:41.941 2584-2584/com.genericappname I/Core: onEnable in ENABLING
01-13 10:28:41.941 2584-2584/com.genericappname D/Core: SDK state change from ENABLING to ENABLED
01-13 10:28:41.941 2584-3650/com.genericappname E/Core: ***Exec Runnable***
01-13 10:28:41.941 2584-3650/com.genericappname D/Core: ThisId: 0 thisExpectedId: 23
01-13 10:28:42.241 2584-2584/com.genericappname D/Core: enable ENABLED
01-13 10:28:42.251 2584-3694/com.genericappname E/Core: ***Exec Runnable***
01-13 10:28:45.491 2584-2584/com.genericappname V/ActivityThread: updateVisibility : ActivityRecord{38270d8e token=android.os.BinderProxy@177d3607 {com.genericappname/com.genericappname.Demo}} show : true
01-13 10:28:45.501 2584-2584/com.genericappname D/SRIB_DCS: log_dcs ThreadedRenderer::initialize entered!
01-13 10:28:45.501 2584-2669/com.genericappname D/mali_winsys: new_window_surface returns 0x3000, [1440x2560]-format:1
01-13 10:28:45.561 2584-2584/com.genericappname D/ViewRootImpl: changeCanvasOpacity: opaque=false
01-13 10:28:45.891 2584-2584/com.genericappname D/DEMO: onPause
01-13 10:28:45.941 2584-3650/com.genericappname E/Core: ***Exec Runnable***
01-13 10:28:45.951 2584-2584/com.genericappname D/Core: disable
01-13 10:28:45.951 2584-2584/com.genericappname D/Core: SDK state change from ENABLED to DISABLING
01-13 10:28:45.951 2584-2584/com.genericappname I/Core: onDisable
01-13 10:28:45.951 2584-2584/com.genericappname D/Core: SDK state change from DISABLING to INITIALIZED
01-13 10:28:45.951 2584-2584/com.genericappname D/Core: Exec Shutdown
01-13 10:28:46.001 2584-2584/com.genericappname I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@177d3607 time:87045589
01-13 10:28:46.011 2584-2584/com.genericappname D/DEMO: onStop
01-13 10:28:46.011 2584-2584/com.genericappname D/DEMO: onDestroy
01-13 10:28:47.941 2584-3650/com.genericappname E/Core: ***Exec Runnable***
01-13 10:28:47.941 2584-3650/com.genericappname D/Core: ThisId: 0 thisExpectedId: 23
01-13 10:28:49.941 2584-3650/com.genericappname E/Core: ***Exec Runnable***
01-13 10:28:49.941 2584-3650/com.genericappname D/Core: ThisId: 0 thisExpectedId: 23
01-13 10:28:51.941 2584-3650/com.genericappname E/Core: ***Exec Runnable***
01-13 10:28:51.941 2584-3650/com.genericappname D/Core: ThisId: 0 thisExpectedId: 23
01-13 10:28:53.941 2584-3650/com.genericappname E/Core: ***Exec Runnable***
01-13 10:28:53.941 2584-3650/com.genericappname D/Core: ThisId: 0 thisExpectedId: 23
01-13 10:28:55.941 2584-3650/com.genericappname E/Core: ***Exec Runnable***
01-13 10:28:55.941 2584-3650/com.genericappname D/Core: ThisId: 0 thisExpectedId: 23
01-13 10:28:57.951 2584-3650/com.genericappname E/Core: ***Exec Runnable***
01-13 10:28:57.951 2584-3650/com.genericappname D/Core: ThisId: 0 thisExpectedId: 23

最佳答案

在调用 disable 之前第二次调用该方法时会发生什么:

private void enable(){
exec = new ScheduledThreadPoolExecutor(1); // Creates a NEW scheduler and takes the place of the old one. But the old one still exists and does its duty!
long period = 2000;
exec.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
exec.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
sf = exec.scheduleAtFixedRate(new SwitchCheck(), 0, period, TimeUnit.MILLISECONDS);
}

exec 将指向一个新的执行器,第一个执行器将继续执行任务。

您需要检查是否

  1. 您已经有一个尚未关闭的执行程序。
  2. 您已经有了任务计划。如果是这样,enable 就是一个空操作。

关于java - 我的 ScheduledThreadPoolExecutor 没有关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34769210/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com