Android Timer: postDelayed vs. schedule

2020-07-17 06:35发布

Please don't put this on hold, please don't mark as duplicate and please answer me! 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(), ( Android timer? How-to? ).

Updated Questions:

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

-Wich way is most precise and fast if I dont 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 ?

Thanks. Some example...

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

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 cant interact with the UI directlly, 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);
}

标签: android timer
2条回答
够拽才男人
2楼-- · 2020-07-17 07:21

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.
查看更多
Anthone
3楼-- · 2020-07-17 07:30

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);
查看更多
登录 后发表回答