java.util.concurrent.TimeUnit

public enum TimeUnit extends Enum<TimeUnit>

A TimeUnit represents time durations(持续时间) at a given unit of granularity(粒度) and provides utility methods to convert across units, and to perform timing(定时) and delay operations in these units. A TimeUnit does not maintain time information, but only helps organize and use time representations(表述) that may be maintained(保持) separately across various contexts(上下文). A nanosecond(纳秒) is defined as one thousandth of a microsecond(微秒), a microsecond as one thousandth of a millisecond(毫秒), a millisecond as one thousandth of a second, a minute as sixty seconds, an hour as sixty minutes, and a day as twenty four hours.
A TimeUnit is mainly used to inform(告知) time-based methods how a given timing parameter should be interpreted(解释). For example, the following code will timeout in 50 milliseconds if the lock is not available:
  Lock lock = ...;
  if (lock.tryLock(50L, TimeUnit.MILLISECONDS)) ...
while this code will timeout in 50 seconds:
  Lock lock = ...;
  if (lock.tryLock(50L, TimeUnit.SECONDS)) ... 
Note however, that there is no guarantee(保证) that a particular timeout implementation will be able to notice the passage of time at the same granularity(粒度) as the given TimeUnit.

Since:

1.5


sleep

public void sleep(long timeout)  throws InterruptedException
Performs a Thread.sleep using this time unit. This is a convenience method that converts time arguments into the form required by the Thread.sleep method.

Parameters:

timeout - the minimum time to sleep. If less than or equal to zero, do not sleep at all.

Throws:

InterruptedException - if interrupted while sleeping



convert

public long convert(long sourceDuration,TimeUnit sourceUnit)
Convert the given time duration(持续时间) in the given unit to this unit. Conversions(转换) from finer(更精细) to coarser(粗糙) granularities(粒度) truncate(截短), so lose precision(精度). For example converting 999 milliseconds to seconds results in 0. Conversions from coarser to finer granularities with arguments(参数) that would numerically overflow saturate(饱和) to Long.MIN_VALUE if negative or Long.MAX_VALUE if positive.
For example, to convert 10 minutes to milliseconds, use: TimeUnit.MILLISECONDS.convert(10L, TimeUnit.MINUTES)

Parameters:

sourceDuration - the time duration in the given sourceUnit
sourceUnit - the unit of the sourceDuration argument

Returns:

the converted duration in this unit, or Long.MIN_VALUE if conversion would negatively overflow, or Long.MAX_VALUE if it would positively overflow.



package test.concurrent;

import java.util.concurrent.TimeUnit;

public class TestTimeUnit {
	public static void main(String[] args) throws InterruptedException {
		// TimeUnit.DAYS.sleep(1000);
		// TimeUnit.SECONDS.sleep(10);
		// System.out.println("test");

		// 1小时转换成3600秒
		System.out.println(TimeUnit.SECONDS.convert(1, TimeUnit.HOURS));
		
		System.out.println(Long.MAX_VALUE);
		System.out.println(TimeUnit.MILLISECONDS.convert(10l, TimeUnit.MINUTES));
		
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值