关于Timer的问题

2019-03-25 13:32|来源: 网路

是这样的,我在程序里面用一个timer调用TimerTask,定时每个几个小时执行。在程序开始启动后的几天还行,可是过了几天后就自动不动了。不知道是为什么?具体代码如下:
      TimerTask tt = new TimerTask() {  
                    public void run() { 
                        //下面的线程池被廖博注释掉了,先测测。
                        Runnable runable = new Runnable() {
                            public void run() { 
                                task.collect();
                            }
                        }; 
                        try{    
                        Commons.threadPools.submit(runable);
                        }catch(Exception e){
                            logger.info(e.toString()); 
                        }
                    }

                }; 

timer.scheduleAtFixedRate(tt, 0, 6*60*60*1000);

里面也没有什么资源没有关闭,因为该程序启动以后只是从ftp上取文件下来,之后就关闭了。不知道为什么会出现这种情况。

问题补充:
beneo 写道
ScheduledThreadPoolExecutor,Timer不推荐使用

为什么,能给我详细讲讲吗?或者给我说那里有相关资料给我说下,好吗?谢谢!!!

问题补充:
beneo 写道
tem 68 in Effective Java (2nd ed) recommends ScheduledThreadPoolExecutor as a more flexible replacement for Timer

你可以在 effective java的item#68找到说明,timer只用一个线程来执行任务,这在面对长期任务时,会影响定时的准确性。如果timer唯一的线程抛出未捕获的异常,timer就会停止运行。

更多的你在书里面看好了

哦,好的。谢谢您。

相关问答

更多
  • 根据Java并发实践 : Timer可以对系统时钟的变化敏感, ScheduledThreadPoolExecutor不是。 Timer只有一个执行线程,所以长时间运行的任务可以延迟其他任务。 ScheduledThreadPoolExecutor可以配置任意数量的线程。 此外,您可以完全控制创建的线程(如果需要的话)(通过提供ThreadFactory )。 TimerTask抛出的运行时异常kill一个线程,从而使Timer死机:-( ...即计划任务不再运行afterExecute不仅捕获运行时异常, ...
  • 我会在仍然可编辑的笔记中添加一个.editable类,不再删除它。 您可以在data-time =“1359999066”属性中保留注释的时间(假设它是unix时间戳)。 如果您需要我暗示的代码的进一步帮助,请告诉我。 (function checkTimer() { $('.note.editable').each(function() { //check for timestamp, remove .editable and hide button }); if ...
  • shcedule(task, 0, 2000) - 使用3参数方法。 第二个参数是延迟,第三个是句点。 见Timer#schedule(..) shcedule(task, 0, 2000) - use the 3-argument method. The second argument is the delay, and the third is the period. See Timer#schedule(..)
  • 我认为你使用的是Windows计时器。 如果这是真的,那么您的第一个消息框会产生不必要的副作用,以在程序的消息队列中累积许多Tick事件。 您有更多时间留在视频中,并显示消息框,将显示更多消息框,因为消息会累积在队列中 您可以尝试使用全局表单变量来解决这种情况 Dim timeElapsed as Boolean Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick if timeElapsed ...
  • 我不知道你想达到什么目的,但是关于你目前的设计,这里有几点: 您的GetTimer在多线程模式下被破坏: if (_timer == null) _timer = new UpdaterTimer(); 假设你有两个线程,每个线程同时调用GetTimer,第一个线程检查_timer并且发现它为null,所以它继续。 但是在那个时候,在它到达_timer = new UpdateTimer() ,线程上下文切换切换到另一个线程并暂停当前线程的执行。 所以另一个线程检查_timer并且发现它不为null,所以它 ...
  • 这不是你如何使用定时器 - 这是不可能的这样执行停止。 实际的错误告诉你,你不能投100到Timer类型的对象,那是因为你写的实际上是一个强制转换。 如果你想创建对象的话 new Timer(100); (所以你忘了新的) 一个关于如何使用Timer类的例子。 This is not how you use a Timer - it is not possible to make the execution stop in this way. The actual error tells you that ...
  • 您应该将number声明为类字段,而不是方法的本地变量。 这样它就不需要是最终的,可以在匿名内部类中使用。 我建议它不要是静态的,你不要在静态环境中使用你的Timer,而是在实例世界中。 public class Main{ private int number = 0; public void someNonStaticMethod() { //some code // final int number = 0; numberLabel.setTex ...
  • 您可以在Linux上使用基于块的计时器功能。 这是一个最小的自包含示例,它在Xcode 9.1和https://swift.sandbox.bluemix.net/#/repl中编译和运行: import Foundation import CoreFoundation let timer = Timer.scheduledTimer(withTimeInterval: 2.0, repeats: false) { timer in print("In timer function") e ...
  • 第一个 * :这是扩展 * CountDownTimer : public class GameTimer extends CountDownTimer{ private YourActivity context; private int timer = 120; private int endtime; private TextView timer1,endTime1; public gameTimer(YourActivity context, long startTime, long interv ...
  • start()怎么样? 它应该重新启动计时器,否则将不需要restart() 。 What about start()? It should restart the timer, as there would be no need for restart() otherwise.