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***");
}
}
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将指向一个新的执行器,第一个执行器将继续执行任务。
你需要检查一下
java.util.concurrent.ScheduledThreadPoolExecutor是ThreadPoolExecutor的子类,并且可以另外调度命令以在给定延迟之后运行,或者定期执行。 ScheduledThreadPoolExecutor方法 Sr.No. 方法和描述 1 protected 《V》 RunnableScheduledFuture《V》 decorateTask(C
我有一个预定的执行者服务,我要求在预定任务后返回期货。Javadoc说在调度任务后返回ScheduledFuture—但是,如何获得尚未运行任务的后续未来? 我的调用如下所示:exec.scheduleAtFixed速率(新任务(),0,间隔,时间单位。分钟); 新任务确实已提交并运行,但我只有第一次打电话时才有未来。这个未来是可重用的还是可重用的?
我在理解ScheduledExecutorService/ScheduledThreadPoolExecutor的具体工作方式时遇到一些问题。我想进行以下设置: 一个类,它设置ScheduledThreadPoolExecutor,在调度任务之前将RemoveOnCancelPolicy设置为true,并关闭ScheduledThreadPoolExecutor(因为我想在try-catch块中调
问题内容: 我仍然是Java的初学者,所以我对线程和并发的了解还很少。但是,由于java.util.Timer和TimerTask存在问题,我希望能够将ScheduledThreadPoolExecutor用作计时器。我对线程的创建非常感兴趣,并且知道我将在几周内学习它们。但是,如果可以的话,有人可以给我一个基本的示例,说明如何使用util.timer将当前的迷你测试程序转换为ScheduledT
主要内容:实例是的子类,并且可以额外地调度在给定延迟之后运行的命令,或定期执行。 实例 以下程序显示了基于线程的环境中接口的使用。 这将产生以下结果 -
我在我的活动中使用了一个倒计时计时器来实现一些倒计时功能。我决定离开CountDownTimer,使用ScheduledThreadPoolExecutor,因为CountDownTimer不能在onTick()中自动取消。 出于某种原因,以下代码中的my只执行一次。我不知道为什么它没有执行多次。函数未被击中。