Handler vs Timer
In our Android development process, we often need to perform a few short-cycle scheduled tasks, when there are two select timer or handler. However, the individual thinks: Handler is more excellent than the timer in many aspects, it is recommended.
I. Ease-of-use
Repeatable handler can perform a task repeatedly. A timer will throw a IllegalStateException exception if it is executed again after a task is executed/canceled. To avoid this exception, you need to recreate a timer object.
Cycle Adjustable
If you want to perform an increasingly fast scheduled task, handler can do it, and the timer consumes more. Handler
-
Private New Handler (); int mdelaytime = +; Private New Runnable () { publicvoid run () { update (); if (Mdelaytime > 0) { handler.postdelayed (this, mdelaytime); -=;}} ; Handler.postdelayed (runnable,1000);
As the above example, the dynamic adjustment of the cycle can be achieved. The timer's scheduleatfixedrate (timertask task, long delay, long period) can only perform fixed-cycle tasks, so it is not possible to dynamically adjust the cycle. If you want to
To adjust dynamically, you need to set a new time after playing a timer task and then starting a new task.
UI Update Handler: You can specify the thread at the time of creation, typically built in activity, the main thread, so you can easily update it in the callback method
Interface. Timer: Asynchronous callback, so you have to use handler to update the interface, inconvenient. Since all have to use handler to update the interface, why not the timing of the function is also handed
To give
Handler to do it?
Two. Memory ratio
A timer takes up more memory than handler. The next demo example loops through two ways to print the log, and then through the mat plugin to see the objects that are required to be called by the two classes.
Percentage
intMCount = 0;Private voidStarttimer () {Timer timer=NewTimer (); Timer.schedule (NewTimerTask () {@Override Public voidrun () {LOG.I ("JKL", "" "+mCount); MCount++; } }, 0, 200);}Private voidStarthandler () {FinalHandler Mhandler =NewHandler (); Mhandler.post (NewRunnable () {@Override Public voidrun () {LOG.I ("JKL", "" "+mCount); MCount++; Mhandler.postdelayed ( This, 200); } });}
You can test by creating a new Android project by calling either of the Starttimer or Starthandler methods in the OnCreate method. The following are the test results for mat.
Memory ratio of timer-related objects
A total of 5 objects, occupying 1192B. Handler memory proportion of related objects
We can see there are 2 handler on it, will you be wondering? In fact, one is our generated handler, and the other is the activity by default generated, exists in the Viewrootimp, which involves viewtree knowledge, here is inconvenient to expand.
However, we can know that we build our own handler, at most, it only occupies 64B.
In the use of handler, but also need to use the runnable, but also only accounted for 16B. So, using handler as a timer, up to 80B.
The above can be concluded that compared to the timer mode of the timers Occupy 1192b,handler way occupy resources will be much smaller, only 1/60.
So the handler way is more memory-saving.
Write here, think of a point, the timer is to create a thread to count, and handler is running in the default main thread. If handler also use an asynchronous thread to run, will it consume a lot of resources? The following is the test code:
Private void Starthandler () { new handlerthread ("Test"); Thread.Start (); Final New Handler (Thread.getlooper ()); Mhandler.post (new Runnable () { @Override publicvoid run () { log.i ("JKL", "" "+ MCount); MCount+ +; Mhandler.postdelayed (this, $);}} );}
above use a handlerthread to start a new thread. And look at the memory ratio.
To see Handlerthread also just accounted for 96B of memory.
In summary, handler memory ratio is low! And it's a lot lower.
From ease of use or memory footprint, handler
Android Handler vs Timer