Android TimeUnit indicates the time period of the given unit Granularity

Source: Internet
Author: User

Android TimeUnit indicates the time period of the given unit Granularity
Public enum TimeUnit
Extends Enum
TimeUnit indicates the time period of a given unit granularity. It provides a practical tool for cross-unit conversion and timing and latency operations in these units. TimeUnit does not maintain time information, but helps organize and use time representations that may be maintained independently across various contexts.
TimeUnit is mainly used to notify the time-based method of how to explain the given timing parameters. . For example, if the lock is unavailable, the following code times out after 50 milliseconds:
Lock lock = ...;
If (lock. tryLock (50L, TimeUnit. MILLISECONDS ))...
The following code times out in 50 seconds:
Lock lock = ...;
If (lock. tryLock (50L, TimeUnit. SECONDS ))...
However, it is not guaranteed that the specific timeout implementation can use the same granularity of notification segments as the given TimeUnit.
Start with the following versions:
1.5
Enumeration constant Summary
MICROSECONDS: 1 million s per second (MS/1000)
MILLISECONDS: 1‰ seconds in MILLISECONDS
NANOSECONDS: one thousandth of a second (microsecond/1000)
SECONDS
MINUTES
HOURS hour
DAYS
Main interfaces:
Long convert (long duration, TimeUnit unit)
Converts the time period of a given unit to this unit.
Void sleep (long timeout)
Use this unit to execute Thread. sleep. This is a convenient way to convert the time parameter to the format required by the Thread. sleep method.
Void timedJoin (Thread thread, long timeout)
Use this time unit to execute the Thread. join for timing.
Void timedWait (Object obj, long timeout)
The Object. wait that uses this time unit for timing.
Long toMicros (long duration)
It is equivalent to MICROSECONDS. convert (duration, this ).
Long toMillis (long duration)
It is equivalent to MILLISECONDS. convert (duration, this ).
Long toNanos (long duration)
It is equivalent to NANOSECONDS. convert (duration, this ).
Long toSeconds (long duration)
It is equivalent to SECONDS. convert (duration, this ).
Static TimeUnit valueOf (String name)
Returns an enumerated constant of the specified type.
Static TimeUnit [] values ()
Returns an array containing constants in the declared order of constants of this enumeration type.
Note 1: Its constants, such as MICROSECONDS, MILLISECONDS, NANOSECONDS, and SECONDS, are of the TimeUnit type.
Note 2 : Convert (long duration, TimeUnit unit) means to convert the duration time to the time format represented by this object (this.
This object may be MICROSECONDS, MILLISECONDS, NANOSECONDS, or SECONDS.
For the duration format (MICROSECONDS, MILLISECONDS, NANOSECONDS, and SECONDS ),
It is determined by the TimeUnit parameter.
Note 3 : TimeUnit is an enum (enumeration) type and cannot be instantiated externally. .
For users, only constants defined internally can be used: MICROSECONDS, MILLISECONDS, NANOSECONDS, SECONDS, MINUTES, HOURS, DAYS
Note 4: For constants MINUTES, HOURS, DAYS, they are not seen in Java and Android, but in Java 1.6.
Note 5: For more information about enumeration types, see 《 Enumeration type" Instance 1:
Class Task implements Delayed
{
String name;
Long submitTime;
Task (String taskName, long delayTime)
{
Name = taskName;
/* Conver the time from MILLISECONDS to NANOSECONDS
**
*/
SubmitTime = TimeUnit. NANOSECONDS. convert (submitTime, TimeUnit. MILLISECONDS) + System. nanoTime ();
}
Public long getDelay (TimeUnit unit)
{
System. out. println ("get delay ");
Return unit. convert (submitTime-System. nanoTime (), TimeUnit. NANOSECONDS );
}
Public int compareTo (Delayed o)
{
System. out. println ("compareTo ");
Task that = (Task) o;
Return submitTime> that. submitTime? 1 :( submitTime <that. submitTime? -1: 0 );
}
Void doTask ()
{
System. out. println ("do task:" + name );
}
}

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.