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);
}
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 itsLooper
's thread which may or may no be a UI thread. Generally speaking there's no much difference between this two classes if you useHandler
on a separate thread. But it's more common in Android to useHandler
andHandlerThread
.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 anActivity
subclass.Handler
,Timer
andScheduledExecutorService
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:
ScheduledExecutorService
if you need a task to run on a separate thread or/and the task is too heavy for the main thread.Handler
for running tasks on the main thread or if you need its message queue functionality.Timer
, useScheduledExecutorService
instead.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.