Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Timer: postDelayed vs. schedule

Tags:

android

timer

I need stuff to be done every 200 milliseconds and I need it to be smooth and even. The task to do is medium-weight(in both cases, of the question 1 and 2). Searching for timers look like the best solution from the community is to use an handler, a runnable and postDelayed(), ( How to set timer in android? ).

Updated Questions:

-Which way is most precise and fast if I have to interact with the UI, postDelayed() or schedule()/scheduleAtFixedRate()?

-Which way is most precise and fast if I don't have to interact with the UI, postDelayed() or schedule()/scheduleAtFixedRate()?

-Both ways could be executed on the main process or on a separate one? how?

This way I have the mainclass local objects, but is it a new thread? Is it a good practice?

timer.schedule( new TimerTask() {           
        @Override
        public void run() {
                    textview.setText(str);
                    //other stuff            
                                     }
            }, 0, 200);

The next is another with schedule, the task is in a separate task, not the timer itself, I guess, and I can't interact with the UI directly, I guess...

timer.schedule( new aclass(), 0, 200);

Handler way(from Dave.B):

Handler timerHandler = new Handler();
Runnable timerRunnable = new Runnable() {
    @Override
    public void run() {
        TextView.setText(str);
        // other stuff
        timerHandler.postDelayed(this, 500); }
};

@Override
public void onCreate(Bundle savedInstanceState) {
...
timerHandler.postDelayed(timerRunnable, 0);
}
like image 913
Jackd Avatar asked Sep 12 '14 02:09

Jackd


2 Answers

Timer executes its tasks on a separate thread that is used only for serving tasks created by this particular timer. Handler runs its task on its Looper's thread which may or may no be a UI thread. Generally speaking there's no much difference between this two classes if you use Handler on a separate thread. But it's more common in Android to use Handler and HandlerThread.

If you need to interact with UI, you'd better use Handler on the main thread in order to avoid context switching. But in this case there may be some delays because performance of such timer will depend on the overall performance of the UI.

UPDATE: In the third example the Runnable is executed on the main thread. I assume that all the code in this example is located in an Activity subclass.

Handler, Timer and ScheduledExecutorService use the similar approach to scheduling tasks. I'm not sure you can get significant changes in precision or performance when switching from one implementation to another.

UPDATE: Finally, I would recommend you the following approach to choosing a timer:

  1. Use ScheduledExecutorService if you need a task to run on a separate thread or/and the task is too heavy for the main thread.
  2. Use Handler for running tasks on the main thread or if you need its message queue functionality.
  3. Do not use Timer, use ScheduledExecutorService instead.
like image 91
Michael Avatar answered Sep 28 '22 07:09

Michael


I will rather use ScheduledThreadPoolExecutor because timer runs its task sequentially so if there are more than one tasks at a time, remaining tasks will get delayed. While ScheduledThreadPoolExecutor have pool of threads & it lets multiple tasks run in parallel, so no delay will be there.

Handler postdelayed method is for posting your runnable to the main UI thread in Android,You need it for doing all UI related stuff. Don't confuse timer with Handler postdelayed

Edit 1: For UI related stuff you can use handler postdelayed. e.g.

handler.postDelayed(new Runnable() {
        public void run() {
          textview.setText(str);
          //other UI related stuff      
        }
    }, 200);
like image 43
Akhil Avatar answered Sep 28 '22 07:09

Akhil