Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using single Timer vs multiple Timers for scheduling TimerTasks in Android

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.

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:

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:

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);
    }
}
like image 408
Safa Kadir Avatar asked Apr 28 '17 07:04

Safa Kadir


People also ask

What is the relationship between Timer and TimerTask?

Timer provides method to schedule Task where the task is an instance of TimerTask class, which implements the Runnable interface and overrides run() method to define task which is called on scheduled time.

Does Java Timer create a new thread?

yes,Java Timer object can be created to run the associated tasks as a daemon thread.

What is delay and period in Timer Android?

delay - delay in milliseconds before task is to be executed. period - time in milliseconds between successive task executions. (Your IDE should also show it to you automatically) So delay is the time from now till the first execution, and after that it executes every period milliseconds again.

What is Timer schedule?

The schedule(TimerTask task,long delay,long period) method is used to schedule the specified task for repeated fixed-delay execution, beginning after the specified delay.


1 Answers

The first solution creates one timer object; the second one multiple timers. As the javadoc clearly explains, each timer comes with its own thread:

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.

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.

like image 69
GhostCat Avatar answered Oct 29 '22 16:10

GhostCat