Android Timeunit represents the time period for the granularity of a given cell

Source: Internet
Author: User

Public enumTimeunit
extends Enum<timeunit>
Timeunit represents a time period for a given cell granularity, which provides a utility method for cross-cell conversion and execution of timing and delay operations within these cells. Timeunit does not maintain time information, but it helps organize and use time representations that may be maintained separately across various contexts.
Timeunit is primarily used to inform the time-based approach how to interpret a given timing parameter. For example, if lock is not available, the following code will time out after 50 milliseconds:
lock lock = ...;
if (Lock.trylock (50L, timeunit.milliseconds)) ...
The following code will time out after 50 seconds:
lock lock = ...;
if (Lock.trylock (50L, timeunit.seconds)) ...
Note, however, that a specific timeout implementation is not guaranteed to have the same granularity notification segment as the given Timeunit.
Start from the following versions:
1.5
Enumeration Constants Summary
microsecondsMicroseconds one out of 10,000 seconds (milliseconds/1000)
MILLISECONDSMilliseconds 1 per thousand seconds
nanosecondsnanosecond One-zero seconds (is microsecond/1000)
SECONDSSeconds
MINUTESMinutes
HOURSHours
DaysDays
Main interface:
Long Duration, Timeunit unit
Converts the time period for the order element to this cell.
void sleep (Long timeout)
Use this cell to perform 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 perform timing thread.join.
void timedwait (Object obj, long timeout)
Use this time unit to perform timing object.wait.
Long Tomicros (long duration)
Equivalent to Microseconds.convert (duration, this).
Long Tomillis (long duration)
Equivalent to Milliseconds.convert (duration, this).
Long Tonanos (long duration)
Equivalent to Nanoseconds.convert (duration, this).
Long Toseconds (long duration)
Equivalent to Seconds.convert (duration, this).
Static Timeunit valueOf (String name)
Returns an enumeration constant of the type with the specified name.
Static timeunit[] VALUES ()
Returns an array containing these constants in the order of the constants that declare the enumerated type.
Note 1: Its constant microseconds,milliseconds,nanoseconds,seconds are all timeunit types
NOTE 2: The meaning of convert (long duration, Timeunit unit) duration this time into the time form represented by this object (this).
One of the possible microseconds,milliseconds,nanoseconds,seconds of this object.
As to what kind of time duration is (a kind of microseconds,milliseconds,nanoseconds,seconds),
is determined by the parameter Timeunit unit.
Note 3:Timeunit is an enum (enum) type and cannot be instantiated externally。
Only the following constants that are internally defined are available to the user: Microseconds,milliseconds,nanoseconds,seconds,minutes,hours,days
Note 4: For constant minutes,hours,days, not seen in Java1.5 and Android, but in Java1.6.
Note 5: Knowledge about enumeration types can be used in the Enum Type》Example 1:
classTaskImplementsDelayed
{
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 Longgetdelay (timeunit unit)
 {  
System.out.println ("Get delay");
         returnUnit.convert (Submittime-system.nanotime (), timeunit.nanoseconds);
  }  
   public intcompareTo (Delayed o)
    {  
System.out.println ("CompareTo");
Task that = (Task) o;
         returnsubmittime > That.submittime?1: (Submittime < that.submittime -1:0);
    } 
 voiddotask ()
 {
System.out.println ("Do task:" +name);
 }
}

Android Timeunit represents the time period for the granularity of a given cell

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.