计时器时间表与scheduleAtFixedRate? [英] Timer schedule vs scheduleAtFixedRate?

查看:136
本文介绍了计时器时间表与scheduleAtFixedRate?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class MyTimerTask extends TimerTask{

    @Override
    public void run() {
        int i = 0; 
        try {
            Thread.sleep(100000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Run Me ~" + ++i);
        System.out.println("Test");

    }

}

Case 1 :-
    TimerTask task = new MyTimerTask();
    Timer timer = new Timer();
    timer.schedule(task, 1000,6000); // line 1
    System.out.println("End"); // here is bebug point. 

我对schedule()方法的期望(根据我在javadocs中给出的理解,在每次执行完成后,将安排每次执行) 那两个线程应该是 在第1行之后创建.

My Expectation of schedule() method (as per my understanding given in javadocs where each execution is scheduled once previous task execution is completed) that two threads should be created after line 1.

timer的一个,它产生另一个任务线程.一旦第一个任务线程死亡 另一个将被创建并继续下去.但是在调试点,我只看到一个与Timer相对应的线程.为什么 没有线程来执行Runnable的任务?

One for timer which spawns another thread for tasks. Once first task thread dies another will be created and son on. But at debug point , i just see one thread corresponding to Timer. Why not thread for tasks which implement Runnable?

Case 2 :-

    TimerTask task = new MyTimerTask();
    Timer timer = new Timer();
    timer.scheduleAtFixedRate(task, 1000,6000); // line 1
    System.out.println("End"); // here is bebug point. 

我对scheduleAtFixedRate()方法的期望(根据我在javadocs中给出的理解,其中,每个执行都相对于已调度的已调度 初始执​​行的执行时间)大约17个线程(不要太在意) 到17..但应大于2)应为 在第1行之后创建.

My Expectation of scheduleAtFixedRate() method(as per my understanding given in javadocs where each execution is scheduled relative to the scheduled execution time of the initial execution) that around 17 threads(dont pay much attention to 17. It can be more or less to that. But it should be greater than 2 ) should be created after line 1.

timer的一个,它应产生对应于每个任务两个的其他16个线程.最初的任务是睡觉 在100秒内,Timer应该创建与下一个任务相对应的另一个线程,对于其他任务也是如此. 但是在调试点,我只看到一个与Timer相对应的线程.在这里我也可以看到任务的顺序执行.为什么不使用17个线程?

One for timer which should spawn 16 other thread corresponding two each task. At first task sleeps for 100 second, Timer should create another thread corresponding to next task and similarly for other task. But at debug point , i just see one thread corresponding to Timer. Here also i can see sequential execution of task. Why not 17 threads?

更新:-按照对我来说,如果第二个任务到期,即使第一个任务未完成,也会给计时器留下印象适当任务的新线程.不是吗?

UPDATE :- As per ScheduleAtFixedRate javadocs , each execution is scheduled relative to the scheduled execution time of the initial execution. If an execution is delayed for any reason (such as garbage collection or other background activity), two or more executions will occur in rapid succession to "catch up. what does that mean? To me it gives impression, if second task is due even first task is not completed, then timer will create new thread for due task. Is n't it?

推荐答案

Timer 的javadoc说

The javadoc for Timer says

与每个Timer对象相对应的是一个后台线程,该线程 用于依次执行计时器的所有任务.

Corresponding to each Timer object is a single background thread that is used to execute all of the timer's tasks, sequentially.

基本上,它拥有一排任务,在您计划任务时会添加到队列中.它使用一个线程来遍历队列并执行任务.

Basically it holds a queue of tasks to which it adds when you schedule them. It uses one thread to iterate over the queue and execute the tasks.

这篇关于计时器时间表与scheduleAtFixedRate?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆