I think I don't fully understand how the Timer and TimerTask work in Java and Android. Now, I have a number of periodic tasks defined, to be scheduled within a timer.
我想我并不完全理解Timer和TimerTask在Java和Android中是如何工作的。现在,我定义了许多定期任务,以便在计时器内进行安排。
I wonder should I use a single timer to schedule tasks, or using different timer instances for each task is ok? Do timers have their own threads? Do scheduled tasks executed in new threads? What is happening in the background?
我想我应该使用单个计时器来安排任务,还是为每个任务使用不同的计时器实例?定时器有自己的线程吗?在新线程中执行预定任务吗?背景中发生了什么?
What is the differences between these approaches?
这些方法之间有什么区别?
Sample code for approach 1:
方法1的示例代码:
private void initTasks() {
TimerTask task1 = new MyTimerTask1();
TimerTask task2 = new MyTimerTask2();
TimerTask task3 = new MyTimerTask3();
Timer timer = new Timer();
timer.schedule(task1, 0, TASK1_PERIOD);
timer.schedule(task2, 0, TASK2_PERIOD);
timer.schedule(task3, 0, TASK3_PERIOD);
}
class MyTimerTask1 extends TimerTask {
public void run() {
//do task1
}
}
class MyTimerTask2 extends TimerTask {
public void run() {
//do task2
}
}
class MyTimerTask3 extends TimerTask {
public void run() {
//do task3
}
}
Sample code for approach 2:
方法2的示例代码:
private void initTasks() {
MyTimerTask1 task1 = new MyTimerTask1();
MyTimerTask2 task2 = new MyTimerTask2();
MyTimerTask3 task3 = new MyTimerTask3();
task1.start();
task2.start();
task3.start();
}
class MyTimerTask1 extends TimerTask {
private Timer timer;
public void run() {
//do task1
}
public void start() {
timer = new Timer();
timer.schedule(this, 0, TASK1_PERIOD);
}
}
class MyTimerTask2 extends TimerTask {
private Timer timer;
public void run() {
//do task2
}
public void start() {
timer = new Timer();
timer.schedule(this, 0, TASK2_PERIOD);
}
}
class MyTimerTask3 extends TimerTask {
private Timer timer;
public void run() {
//do task3
}
public void start() {
timer = new Timer();
timer.schedule(this, 0, TASK3_PERIOD);
}
}
1 个解决方案
#1
2
The first solution creates one timer object; the second one multiple timers. As the javadoc clearly explains, each timer comes with its own thread:
第一个解决方案创建一个计时器对象第二个多个计时器。正如javadoc清楚地解释的那样,每个计时器都有自己的线程:
Corresponding to each Timer object is a single background thread that is used to execute all of the timer's tasks, sequentially. Timer tasks should complete quickly. If a timer task takes excessive time to complete, it "hogs" the timer's task execution thread. This can, in turn, delay the execution of subsequent tasks, which may "bunch up" and execute in rapid succession when (and if) the offending task finally completes.
对应于每个Timer对象的是一个后台线程,用于按顺序执行所有计时器的任务。计时器任务应该快速完成。如果计时器任务需要花费过多的时间来完成,它会“占用”计时器的任务执行线程。反过来,这可以延迟后续任务的执行,后续任务可以在紧急任务最终完成时(以及如果)快速连续地“聚集”并执行。
Thus: the second solution guarantees that there are three threads for your three tasks. In other words: you use more resources, but as a result of doing so, you can be sure that your different tasks are not "blocking" each other (when two tasks need to be run on the same thread, well: it is only one thread; it can't execute two tasks in parallel!)
因此:第二个解决方案保证三个任务有三个线程。换句话说:你使用了更多的资源,但是由于这样做,你可以确保你的不同任务不会相互“阻塞”(当两个任务需要在同一个线程上运行时,那么:它只是一个线程;它不能并行执行两个任务!)
But beyond that: when I read around the many answers that a simple search for "java timer android" shows, the real answers seems to be: on Android, you should prefer to use a Handler instead of Timer objects.
但除此之外:当我阅读简单搜索“java timer android”的许多答案时,真正的答案似乎是:在Android上,你应该更喜欢使用Handler而不是Timer对象。
#1
2
The first solution creates one timer object; the second one multiple timers. As the javadoc clearly explains, each timer comes with its own thread:
第一个解决方案创建一个计时器对象第二个多个计时器。正如javadoc清楚地解释的那样,每个计时器都有自己的线程:
Corresponding to each Timer object is a single background thread that is used to execute all of the timer's tasks, sequentially. Timer tasks should complete quickly. If a timer task takes excessive time to complete, it "hogs" the timer's task execution thread. This can, in turn, delay the execution of subsequent tasks, which may "bunch up" and execute in rapid succession when (and if) the offending task finally completes.
对应于每个Timer对象的是一个后台线程,用于按顺序执行所有计时器的任务。计时器任务应该快速完成。如果计时器任务需要花费过多的时间来完成,它会“占用”计时器的任务执行线程。反过来,这可以延迟后续任务的执行,后续任务可以在紧急任务最终完成时(以及如果)快速连续地“聚集”并执行。
Thus: the second solution guarantees that there are three threads for your three tasks. In other words: you use more resources, but as a result of doing so, you can be sure that your different tasks are not "blocking" each other (when two tasks need to be run on the same thread, well: it is only one thread; it can't execute two tasks in parallel!)
因此:第二个解决方案保证三个任务有三个线程。换句话说:你使用了更多的资源,但是由于这样做,你可以确保你的不同任务不会相互“阻塞”(当两个任务需要在同一个线程上运行时,那么:它只是一个线程;它不能并行执行两个任务!)
But beyond that: when I read around the many answers that a simple search for "java timer android" shows, the real answers seems to be: on Android, you should prefer to use a Handler instead of Timer objects.
但除此之外:当我阅读简单搜索“java timer android”的许多答案时,真正的答案似乎是:在Android上,你应该更喜欢使用Handler而不是Timer对象。