The difference between a timer and a Alarmmanager

Source: Internet
Author: User

Thread

This thread is in the ready (executable) state by invoking the start () method of the thread class to start a thread. However, it does not execute at this time, it requires a CPU chip.

Once you get the CPU time slice. The run () method is executed.

The method body of run () is called the thread body, which includes the contents of the thread to be executed , and the Run () method executes the end. This thread also terminates.


Thread state transitions

first, newly built (new) : A new Thread object was created.
Second, executable (runnable) : After a thread object is created, another thread (for example, the main thread) invokes the object's start () method.

The thread of the state is in the executable thread pool, waiting for the thread to be dispatched to be selected. Get the right to use the CPU.
III. Implementation (running) : Executable State ( runnable) thread gets the CPU time slice (timeslice). Run the program code.

Iv. Blockage (block) : A blocking state refers to a thread that has given up CPU usage for some reason. Also let out the CPU timeslice, temporarily stop execution. Until the thread enters the executable (runnable) state. To get the CPU TimeSlice go to Execution (running) state again. There are three types of clogging:

    • waiting for clogging: Execution (running) thread to execute the o.wait () method. The JVM puts the thread into the waiting queue (waitting.

    • Synchronous Blockage: Execution (running) when a thread acquires a synchronization lock on an object, the JVM puts the thread into the lock pool if the synchronization lock is occupied by another thread.
    • Other blockages: Execution (running) thread executes the thread.sleep (long ms) or T.join () method, or when an I/O request is made, the JVM will place the thread in a blocked state. When the sleep () state times out, join () waits for the thread to terminate or time out, or the I/O processing completes, the thread is again transferred to the executable (runnable) state.

v. Death (dead) : The thread Run (), main () method runs the end. or the Run () method is exited because of an exception, the thread ends the life cycle. The thread of death cannot be resurrected again.


Timer

There are two commonly used timers on the Android platform:

    1. Java's timer
    2. Android's Alarmmanager

Timer

The Java Timer class can be used to schedule tasks that need to be run in a loop.


To put it simply,a timer inside the package was installed "a thread "and "a timertask queue ", the queue handles the task in a certain way. The encapsulated Thread isTimerconstructor is invoked, the thread's Run method loops the timertask queue According to the conditions, and then calls the TimerTask Run method.


However, if the CPU goes into hibernation, the thread will be blocked by the loss of CPU time slices. This results in the failure of the scheduled task we need. Scenario analysis of the above timed task failure: If the condition of the timed task is to the time Xx:yy ability to run. However, because CPU hibernation causes a thread clogging relationship, the current system time exceeds this time, even if the CPU is recovered from the terminal, because the condition is not satisfied . Timed tasks naturally fail at this time.


The workaround is that it needs to use Wakelock to keep the CPU awake.

Then the problem comes. This will consume a lot of phone power, greatly reducing the phone standby time. This is not a way to meet our needs.


Note: TimerTask implements the Runnable interface, but its run method is simply called in the thread that is encapsulated in the timer and does not run in another thread. This means that the timer is single-threaded. so the question is, why bother wrapping a runnable interface without multithreaded calls? And my answer is. Foreigners just want to show that it is a way to run.


Alarmmanager

The Alarmmanager is an Android-encapsulated module for managing the RTC, and the RTC (Real time Clock) is a separate hardware clock. Ability to perform normally when the CPU sleeps, and wake the CPU at a preset time when it arrives by interruption. This means that, assuming we use Alarmmanager to perform tasks on a regular basis, the CPU is able to hibernate normally, only to wake up for a very short time when the task needs to be performed.


The difference between a timer and a Alarmmanager

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.