我有一个安装了ScheduledThreadPoolExecutor的Android库。它每两秒执行一次任务以检查条件,并在应用程序调用库的enable()函数时启动它。当应用程序调用disable()函数时,该函数会取消ScheduledFuture任务并在执行程序上调用shutdownNow()。问题是它没有停止。在应用程序的日志中,我可以看到正在调用disable()并查看到达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的地方是我离开活动的地方。
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
答案 0 :(得分:1)
在调用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将指向一个新的Executor,第一个将继续执行任务。
您需要检查是否
enable
就是无操作。