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_CHANGED
AndACTION_TIMEZONE_CHANGED
Intent
Broadcasts 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.
TheHandler
Class can schedule asynchronous callbacks at an absolute or relative time. Handler objects also useuptimeMillis()
Clock, and requireevent loop
(Normally present in any GUI application ).
TheAlarmManager
Can 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 causeIntent
Broadcast when they occur.