publicenumTimeUnit
extendsEnum<TimeUnit><wbr style="line-height:25px"><br style="line-height:25px"><span style="color:#003366; line-height:25px">TimeUnit表示给定单元粒度的时间段,它提供在这些单元中进行跨单元转换和执行计时及延迟操作的实用工具方法。TimeUnit不维护时间信息,但是有助于组织和使用可能跨各种上下文单独维护的时间表示形式。</span><br style="line-height:25px"><wbr style="line-height:25px"><span style="color:#000080; line-height:25px">TimeUnit主要用于通知基于时间的方法如何解释给定的计时参数</span><wbr style="line-height:25px">。 <div style="line-height:25px">例如,如果lock不可用,则以下代码将在50毫秒后超时:<br style="line-height:25px"><span style="color:#0000ff; line-height:25px">Locklock=...;<br style="line-height:25px"> if(lock.tryLock(50L,TimeUnit.MILLISECONDS))...</span><br style="line-height:25px"> 而以下代码将在50秒后超时:<br style="line-height:25px"><span style="color:#0000ff; line-height:25px">Locklock=...;<br style="line-height:25px"> if(lock.tryLock(50L,TimeUnit.SECONDS))...</span><br style="line-height:25px"> 但是注意,不保证特定超时实现能够以与给定TimeUnit相同的粒度通知段。<br style="line-height:25px"> 从以下版本开始:<br style="line-height:25px"> 1.5<br style="line-height:25px"> 枚举常量摘要<br style="line-height:25px"><span style="color:#993300; line-height:25px">MICROSECONDS</span>微秒一百万分之一秒(就是毫秒/1000)<br style="line-height:25px"><span style="color:#993300; line-height:25px">MILLISECONDS</span>毫秒千分之一秒<br style="line-height:25px"><span style="color:#993300; line-height:25px">NANOSECONDS</span>毫微秒十亿分之一秒(就是微秒/1000)<br style="line-height:25px"><span style="color:#993300; line-height:25px">SECONDS</span>秒<br style="line-height:25px"><span style="color:#993300; line-height:25px">MINUTES</span>分钟<br style="line-height:25px"><span style="color:#993300; line-height:25px">HOURS</span>小时<br style="line-height:25px"><span style="color:#993300; line-height:25px">DAYS</span>天<br style="line-height:25px"><span style="line-height:25px">主要接口</span>:<br style="line-height:25px"> longconvert(longduration,TimeUnitunit)<br style="line-height:25px"> 将给定单元的时间段转换到此单元。<br style="line-height:25px"> voidsleep(longtimeout)<br style="line-height:25px"> 使用此单元执行Thread.sleep.这是将时间参数转换为Thread.sleep方法所需格式的便捷方法。<br style="line-height:25px"> voidtimedJoin(Threadthread,longtimeout)<br style="line-height:25px"> 使用此时间单元执行计时的Thread.join。<br style="line-height:25px"> voidtimedWait(Objectobj,longtimeout)<br style="line-height:25px"> 使用此时间单元执行计时的Object.wait。<br style="line-height:25px"> longtoMicros(longduration)<br style="line-height:25px"> 等效于MICROSECONDS.convert(duration,this)。<br style="line-height:25px"> longtoMillis(longduration)<br style="line-height:25px"> 等效于MILLISECONDS.convert(duration,this)。<br style="line-height:25px"> longtoNanos(longduration)<br style="line-height:25px"> 等效于NANOSECONDS.convert(duration,this)。<br style="line-height:25px"> longtoSeconds(longduration)<br style="line-height:25px"> 等效于SECONDS.convert(duration,this)。<br style="line-height:25px"> staticTimeUnitvalueOf(Stringname)<br style="line-height:25px"> 返回带有指定名称的该类型的枚举常量。<br style="line-height:25px"> staticTimeUnit[]values()<br style="line-height:25px"> 按照声明该枚举类型的常量的顺序,返回包含这些常量的数组。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意1:它的常量MICROSECONDS,MILLISECONDS,NANOSECONDS,SECONDS都是TimeUnit类型<br style="line-height:25px"> 注意2</wbr></span><wbr style="line-height:25px">:convert(longduration,TimeUnitunit)的意思将duration这个时间转化为本对象(this)所表示的时间形式。<br style="line-height:25px"> 本对象可能MICROSECONDS,MILLISECONDS,NANOSECONDS,SECONDS的一种。<br style="line-height:25px"> 至于duration是哪种形式的时间(MICROSECONDS,MILLISECONDS,NANOSECONDS,SECONDS的一种),<br style="line-height:25px"> 则是通过参数TimeUnitunit来确定的<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意3</wbr></span><wbr style="line-height:25px">:<span style="line-height:25px"><wbr style="line-height:25px">TimeUnit是enum(枚举)类型,不能通过外部来实例化</wbr></span><wbr style="line-height:25px">。<br style="line-height:25px"> 对用户来说只有它内部定义的下常量可用:MICROSECONDS,MILLISECONDS,NANOSECONDS,SECONDS,MINUTES,HOURS,DAYS<br style="line-height:25px"> 注意4:对于常量MINUTES,HOURS,DAYS,在Java1.5和Android中并没看到,但是在Java1.6中却有。<br style="line-height:25px"> 注意5:关于枚举类型的知识可参加《<strong><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/17104057920107345213394/" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">枚举类型</a></strong>》</wbr></wbr></wbr> </div> <div style="line-height:25px"> <span style="line-height:25px"><wbr style="line-height:25px">实例1:<br style="line-height:25px"></wbr></span><wbr style="line-height:25px"><span style="color:#993300; line-height:25px">class</span><span style="color:#ff6600; line-height:25px">Task</span><span style="color:#993300; line-height:25px">implements</span>Delayed<br style="line-height:25px"> {<br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Stringname;<br style="line-height:25px"> longsubmitTime;<br style="line-height:25px"> Task(StringtaskName,longdelayTime)<br style="line-height:25px"> {</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">name=taskName;</span><br style="line-height:25px"><span style="color:#808080; line-height:25px">/*converthetimefromMILLISECONDStoNANOSECONDS<br style="line-height:25px"> **<br style="line-height:25px"> */</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">submitTime=TimeUnit.NANOSECONDS.convert(submitTime,TimeUnit.MILLISECONDS)+System.nanoTime();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">publiclong</span><span style="color:#3366ff; line-height:25px">getDelay(TimeUnitunit)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">System.out.println("getdelay");</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">return</span><span style="color:#3366ff; line-height:25px">unit.convert(submitTime-System.nanoTime(),TimeUnit.NANOSECONDS);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"> }</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">publicint</span><span style="color:#3366ff; line-height:25px">compareTo(Delayedo)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">System.out.println("compareTo");</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Taskthat=(Task)o;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">return</span><span style="color:#3366ff; line-height:25px">submitTime>that.submitTime?1:(submitTime<that.submitTime?-1:0);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">void</span><span style="color:#3366ff; line-height:25px">doTask()</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">System.out.println("dotask:"+name);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"> }</wbr> </div> </wbr></wbr></wbr>
extendsEnum<TimeUnit><wbr style="line-height:25px"><br style="line-height:25px"><span style="color:#003366; line-height:25px">TimeUnit表示给定单元粒度的时间段,它提供在这些单元中进行跨单元转换和执行计时及延迟操作的实用工具方法。TimeUnit不维护时间信息,但是有助于组织和使用可能跨各种上下文单独维护的时间表示形式。</span><br style="line-height:25px"><wbr style="line-height:25px"><span style="color:#000080; line-height:25px">TimeUnit主要用于通知基于时间的方法如何解释给定的计时参数</span><wbr style="line-height:25px">。 <div style="line-height:25px">例如,如果lock不可用,则以下代码将在50毫秒后超时:<br style="line-height:25px"><span style="color:#0000ff; line-height:25px">Locklock=...;<br style="line-height:25px"> if(lock.tryLock(50L,TimeUnit.MILLISECONDS))...</span><br style="line-height:25px"> 而以下代码将在50秒后超时:<br style="line-height:25px"><span style="color:#0000ff; line-height:25px">Locklock=...;<br style="line-height:25px"> if(lock.tryLock(50L,TimeUnit.SECONDS))...</span><br style="line-height:25px"> 但是注意,不保证特定超时实现能够以与给定TimeUnit相同的粒度通知段。<br style="line-height:25px"> 从以下版本开始:<br style="line-height:25px"> 1.5<br style="line-height:25px"> 枚举常量摘要<br style="line-height:25px"><span style="color:#993300; line-height:25px">MICROSECONDS</span>微秒一百万分之一秒(就是毫秒/1000)<br style="line-height:25px"><span style="color:#993300; line-height:25px">MILLISECONDS</span>毫秒千分之一秒<br style="line-height:25px"><span style="color:#993300; line-height:25px">NANOSECONDS</span>毫微秒十亿分之一秒(就是微秒/1000)<br style="line-height:25px"><span style="color:#993300; line-height:25px">SECONDS</span>秒<br style="line-height:25px"><span style="color:#993300; line-height:25px">MINUTES</span>分钟<br style="line-height:25px"><span style="color:#993300; line-height:25px">HOURS</span>小时<br style="line-height:25px"><span style="color:#993300; line-height:25px">DAYS</span>天<br style="line-height:25px"><span style="line-height:25px">主要接口</span>:<br style="line-height:25px"> longconvert(longduration,TimeUnitunit)<br style="line-height:25px"> 将给定单元的时间段转换到此单元。<br style="line-height:25px"> voidsleep(longtimeout)<br style="line-height:25px"> 使用此单元执行Thread.sleep.这是将时间参数转换为Thread.sleep方法所需格式的便捷方法。<br style="line-height:25px"> voidtimedJoin(Threadthread,longtimeout)<br style="line-height:25px"> 使用此时间单元执行计时的Thread.join。<br style="line-height:25px"> voidtimedWait(Objectobj,longtimeout)<br style="line-height:25px"> 使用此时间单元执行计时的Object.wait。<br style="line-height:25px"> longtoMicros(longduration)<br style="line-height:25px"> 等效于MICROSECONDS.convert(duration,this)。<br style="line-height:25px"> longtoMillis(longduration)<br style="line-height:25px"> 等效于MILLISECONDS.convert(duration,this)。<br style="line-height:25px"> longtoNanos(longduration)<br style="line-height:25px"> 等效于NANOSECONDS.convert(duration,this)。<br style="line-height:25px"> longtoSeconds(longduration)<br style="line-height:25px"> 等效于SECONDS.convert(duration,this)。<br style="line-height:25px"> staticTimeUnitvalueOf(Stringname)<br style="line-height:25px"> 返回带有指定名称的该类型的枚举常量。<br style="line-height:25px"> staticTimeUnit[]values()<br style="line-height:25px"> 按照声明该枚举类型的常量的顺序,返回包含这些常量的数组。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意1:它的常量MICROSECONDS,MILLISECONDS,NANOSECONDS,SECONDS都是TimeUnit类型<br style="line-height:25px"> 注意2</wbr></span><wbr style="line-height:25px">:convert(longduration,TimeUnitunit)的意思将duration这个时间转化为本对象(this)所表示的时间形式。<br style="line-height:25px"> 本对象可能MICROSECONDS,MILLISECONDS,NANOSECONDS,SECONDS的一种。<br style="line-height:25px"> 至于duration是哪种形式的时间(MICROSECONDS,MILLISECONDS,NANOSECONDS,SECONDS的一种),<br style="line-height:25px"> 则是通过参数TimeUnitunit来确定的<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意3</wbr></span><wbr style="line-height:25px">:<span style="line-height:25px"><wbr style="line-height:25px">TimeUnit是enum(枚举)类型,不能通过外部来实例化</wbr></span><wbr style="line-height:25px">。<br style="line-height:25px"> 对用户来说只有它内部定义的下常量可用:MICROSECONDS,MILLISECONDS,NANOSECONDS,SECONDS,MINUTES,HOURS,DAYS<br style="line-height:25px"> 注意4:对于常量MINUTES,HOURS,DAYS,在Java1.5和Android中并没看到,但是在Java1.6中却有。<br style="line-height:25px"> 注意5:关于枚举类型的知识可参加《<strong><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/17104057920107345213394/" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">枚举类型</a></strong>》</wbr></wbr></wbr> </div> <div style="line-height:25px"> <span style="line-height:25px"><wbr style="line-height:25px">实例1:<br style="line-height:25px"></wbr></span><wbr style="line-height:25px"><span style="color:#993300; line-height:25px">class</span><span style="color:#ff6600; line-height:25px">Task</span><span style="color:#993300; line-height:25px">implements</span>Delayed<br style="line-height:25px"> {<br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Stringname;<br style="line-height:25px"> longsubmitTime;<br style="line-height:25px"> Task(StringtaskName,longdelayTime)<br style="line-height:25px"> {</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">name=taskName;</span><br style="line-height:25px"><span style="color:#808080; line-height:25px">/*converthetimefromMILLISECONDStoNANOSECONDS<br style="line-height:25px"> **<br style="line-height:25px"> */</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">submitTime=TimeUnit.NANOSECONDS.convert(submitTime,TimeUnit.MILLISECONDS)+System.nanoTime();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">publiclong</span><span style="color:#3366ff; line-height:25px">getDelay(TimeUnitunit)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">System.out.println("getdelay");</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">return</span><span style="color:#3366ff; line-height:25px">unit.convert(submitTime-System.nanoTime(),TimeUnit.NANOSECONDS);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"> }</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">publicint</span><span style="color:#3366ff; line-height:25px">compareTo(Delayedo)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">System.out.println("compareTo");</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Taskthat=(Task)o;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">return</span><span style="color:#3366ff; line-height:25px">submitTime>that.submitTime?1:(submitTime<that.submitTime?-1:0);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">void</span><span style="color:#3366ff; line-height:25px">doTask()</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">System.out.println("dotask:"+name);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"> }</wbr> </div> </wbr></wbr></wbr>