
>它只创建单个线程来执行任务和任务
运行时间太长,其他任务受损.
>它不处理由任务抛出的异常,并且线程只是终止,这会影响
其他计划任务,它们永远不会运行.
而另一方面,ScheduledThreadPoolExecutor正确处理所有这些问题,并且使用Timer没有意义..有两种方法可以在你的情况下使用
> scheduleAtFixedrate(…)
> scheduleWithFixedDelay(..)
class LongRunningTask implements Runnable { @OverrIDe public voID run() { System.out.println("Hello world"); } }ScheduledThreadPoolExecutor exec = new ScheduledThreadPoolExecutor(1);long period = 100; // the period between successive executionsexec.scheduleAtFixedrate(new LongRunningTask (),duration,TimeUnit.MICROSECONDS);long delay = 100; //the delay between the termination of one execution and the commencement of the nextexec.scheduleWithFixedDelay(new MyTask(),TimeUnit.MICROSECONDS); 并取消执行者使用此 – ScheduledFuture
// schedule long running task in 2 minutes:ScheduledFuture scheduleFuture = exec.scheduleAtFixedrate(new MyTask(),TimeUnit.MICROSECONDS);... ...// At some point in the future,if you want to cancel scheduled task:scheduleFuture.cancel(true);总结
以上是内存溢出为你收集整理的android – 用于调度固定速率任务的处理程序或计时器全部内容,希望文章能够帮你解决android – 用于调度固定速率任务的处理程序或计时器所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)