I was wondering how the TimerTask is working with threads.
For example, I've got a code that executes a TimerTask, which has a 'run' method which will run on the UI Thread.
class looper extends TimerTask {
public looper() {
}
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
timer.schedule(new looper(), new Date(new Date().getTime() + 100));
}
});
}
}
and the timer would start like this:
timer = new Timer();
timer.schedule(new looper(), new Date());
Will the TimerTask create a new thread? if so, how does runOnUiThread
will work? will it move the code to the UI thread?
I've tried to eliminate the need to call the TimerTask again (timer.schedule
) and just use an infinite loop inside the run
to make calculations - but that would block the UI thread and the app will not respond.
P.S - I must have the code run on the UI thread, because it has to update the UI.
So, what's going on here?