Difference between SystemClock. sleep and Thread. sleep, thread. sleep

Source: Internet
Author: User

SystemClock. sleep and Thread. sleep

 

When we are dealing with Thread Synchronization in Java, processing latency may use the sleep method of the Thread class. Aside from some methods of the concurrent class, the Android platform also provides a SystemClock. what are the differences between sleep methods?

The InterruptedException exception may occur every time we call Thread. sleep, but the SystemClock. sleep method does not. If there is such a description on the SDK, it will ignore the interruption exception.

SystemClock. sleep (millis) is a utility function very similar to Thread. sleep (millis), but it ignores InterruptedException. The following is a reminder:Use this function for delays if you do not use Thread. interrupt (), as it will preserve the interrupted state of the thread.

Three different clocks are available, and they shocould not be confused:

  • System.currentTimeMillis()Is the standard "wall" clock (time and date) expressing milliseconds since the epoch. The wall clock can be set by the user or the phone network (seesetCurrentTimeMillis(long)), So the time may jump backwards or forwards unpredictably. this clock shoshould only be used when corresponsible with real-world dates and times is important, such as in a calendar or alarm clock application. interval or elapsed time measurements shoshould use a different clock. if you are using System. currentTimeMillis (), consider listening toACTION_TIME_TICK,ACTION_TIME_CHANGEDAndACTION_TIMEZONE_CHANGED IntentBroadcasts to find out when the time changes.

  • uptimeMillis()Is counted in milliseconds since the system was booted. this clock stops when the system enters deep sleep (CPU off, display dark, device waiting for external input), but is not affected by clock scaling, idle, or other power saving mechanisms. this is the basis for most interval timing suchThread.sleep(millls),Object.wait(millis), AndSystem.nanoTime(). This clock is guaranteed to be monotonic, and is the recommended basis for the general purpose interval timing of user interface events, performance measurements, and anything else that does not need to measure elapsed time during device sleep. most methods that accept a timestamp value minus CTuptimeMillis()Clock.

  • elapsedRealtime()Is counted in milliseconds since the system was booted, including deep sleep. This clock shocould be used when measuring time intervals that may span periods of system sleep.

There are several mechanisms for controlling the timing of events:

    • Standard functions likeThread.sleep(millis)AndObject.wait(millis)Are always available. These functions useuptimeMillis()Clock; if the device enters sleep, the remainder of the time will be postponed until the device wakes up. These synchronous functions may be interruptedThread.interrupt(), And you must handleInterruptedException.

    • SystemClock.sleep(millis)Is a utility function very similarThread.sleep(millis), But it ignoresInterruptedException. Use this function for delays if you do not useThread.interrupt(), As it will preserve the interrupted state of the thread.

    • TheHandlerClass can schedule asynchronous callbacks at an absolute or relative time. Handler objects also useuptimeMillis()Clock, and requireevent loop(Normally present in any GUI application ).

    • TheAlarmManagerCan trigger one-time or recurring events which occur even when the device is in deep sleep or your application is not running. Events may be scheduled with your choicecurrentTimeMillis()(RTC) orelapsedRealtime()(ELAPSED_REALTIME), and causeIntentBroadcast when they occur.

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.