java.util.concurrent.TimeUnit

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37636695/article/details/79117497
  • TimeUnit提供了可读性更好的线程暂停操作,通常用来替换Thread.sleep(),

在很长一段时间里Thread的sleep()方法作为暂停线程的标准方式,几乎所有Java程序员都熟悉它,事实上sleep方法本身也很常用而且出现在很多面试中。

  • 如果你已经使用过Thread.sleep(),当然我确信你这样做过,那么你一定熟知它是一个静态方法,暂停线程时它不会释放锁,该方法会抛出InterrupttedException异常(如果有线程中断了当前线程),TimeUnit.sleep()内部调用的Thread.sleep()也会抛出InterruptException。
  • 但是我们很多人并没有注意的一个潜在的问题就是它的可读性。Thread.sleep()是一个重载方法,可以接收长整型毫秒和长整型的纳秒参数,这样对程序员造成的一个问题就是很难知道到底当前线程是睡眠了多少秒、分、小时或者天。
  • TimeUnit类解决了这个问题,通过指定DAYS、HOURS、MINUTES,SECONDS、MILLISECONDS和NANOSECONDS。java.utils.concurrent .TimeUnit 是Java枚举应用场景中最好的例子之一,所有TimeUnit都是枚举实例,让我们来看看线程睡眠4分钟用TimeUnit是如何使用的。
    TimeUnit.MINUTES.sleep(4);  // sleeping for 4 minutes
    除了sleep的功能外,TimeUnit还提供了便捷方法用于把时间转换成不同单位,例如,如果你想把秒转换成毫秒,你可以使用下面代码:
    TimeUnit.SECONDS.toMillis(44)
  • TimeUnit常用方法

    //关于秒的常用方法 
    TimeUnit.SECONDS.toMillis(1) 1秒转换为毫秒数 
    TimeUnit.SECONDS.toMinutes(60) 60秒转换为分钟数 
    TimeUnit.SECONDS.sleep(5) 线程休眠5秒 
    TimeUnit.SECONDS.convert(1, TimeUnit.MINUTES) 1分钟转换为秒数 
    
    //TimeUnit.DAYS 日的工具类 
    //TimeUnit.HOURS 时的工具类 
    //TimeUnit.MINUTES 分的工具类 
    //TimeUnit.SECONDS 秒的工具类 
    //TimeUnit.MILLISECONDS 毫秒的工具类

    方法详细信息

    values

    public static final TimeUnit[] values()
    Returns an array containing the constants of this enum type, in the order they are declared. This method may be used to iterate over the constants as follows:
    for(TimeUnit c : TimeUnit.values())
            System.out.println(c);
    
    返回:
    an array containing the constants of this enum type, in the order they are declared

    valueOf

    public static TimeUnit valueOf(String name)
    返回带有指定名称的该类型的枚举常量。 字符串必须与用于声明该类型的枚举常量的 标识符 完全匹配。(不允许有多余 的空格。)
    参数:
    指定要返回的枚举常量的名称。 -
    返回:
    返回带有指定名称的枚举常量
    抛出:
    如果该枚举类型没有带有指定名称的常量, - 则抛出 IllegalArgumentException

    convert

    public long convert(long sourceDuration,
                        TimeUnit sourceUnit)
    将给定单元的时间段转换到此单元。从较细粒度到较粗粒度的舍位转换,这样会失去精确性。例如,将  999 毫秒转换为秒的结果为  0。使用参数从较粗粒度到较细粒度转换,如果参数为负,则在数字上溢出至  Long.MIN_VALUE,如果为正,则为  Long.MAX_VALUE

    例如,要将 10 分钟转换为毫秒,请使用:TimeUnit.MILLISECONDS.convert(10L, TimeUnit.MINUTES)

    参数:
    sourceDuration - 给定  sourceUnit 中的时间段
    sourceUnit -  sourceDuration 参数的单元
    返回:
    此单元中的转换时间段;如果转换将负溢出,则返回  Long.MIN_VALUE;如果转换将正溢出,则返回  Long.MAX_VALUE

    toNanos

    public long toNanos(long duration)
    等效于  NANOSECONDS.convert(duration, this)
    参数:
    duration - 时间段
    返回:
    转换时间段,如果转换将负溢出,则返回  Long.MIN_VALUE;如果转换将正溢出,则返回  Long.MAX_VALUE
    另请参见:
    convert(long, java.util.concurrent.TimeUnit)

    toMicros

    public long toMicros(long duration)
    等效于  MICROSECONDS.convert(duration, this)
    参数:
    duration - 时间段
    返回:
    转换时间段,如果转换将负溢出,则返回  Long.MIN_VALUE;如果转换将正溢出,则返回  Long.MAX_VALUE
    另请参见:
    convert(long, java.util.concurrent.TimeUnit)

    toMillis

    public long toMillis(long duration)
    等效于  MILLISECONDS.convert(duration, this)
    参数:
    duration - 时间段
    返回:
    转换时间段,如果转换将负溢出,则返回  Long.MIN_VALUE;如果转换将正溢出,则返回  Long.MAX_VALUE
    另请参见:
    convert(long, java.util.concurrent.TimeUnit)

    toSeconds

    public long toSeconds(long duration)
    等效于  SECONDS.convert(duration, this)
    参数:
    duration - 时间段
    返回:
    转换时间段;如果转换将负溢出,则返回  Long.MIN_VALUE;如果转换将正溢出,则返回  Long.MAX_VALUE
    另请参见:
    convert(long, java.util.concurrent.TimeUnit)

    toMinutes

    public long toMinutes(long duration)
    等效于  MINUTES.convert(duration, this)
    参数:
    duration - 时间段
    返回:
    转换时间段;如果转换将负溢出,则返回  Long.MIN_VALUE;如果转换将正溢出,则返回  Long.MAX_VALUE
    从以下版本开始:
    1.6
    另请参见:
    convert(long, java.util.concurrent.TimeUnit)

    toHours

    public long toHours(long duration)
    等效于  HOURS.convert(duration, this)
    参数:
    duration - 时间段
    返回:
    转换时间段;如果转换将负溢出,则返回  Long.MIN_VALUE;如果转换将正溢出,则返回  Long.MAX_VALUE
    从以下版本开始:
    1.6
    另请参见:
    convert(long, java.util.concurrent.TimeUnit)

    toDays

    public long toDays(long duration)
    等效于  DAYS.convert(duration, this)
    参数:
    duration - 时间段
    返回:
    转换时间段
    从以下版本开始:
    1.6
    另请参见:
    convert(long, java.util.concurrent.TimeUnit)

    timedWait

    public void timedWait(Object obj,
                          long timeout)
                   throws InterruptedException
    使用此时间单元执行计时的  Object.wait。这是将超时参数转换为  Object.wait 方法所需格式的便捷方法。

    例如,可以使用以下代码实现阻塞 poll 方法(参见 BlockingQueue.poll):

      public synchronized  Object poll(long timeout, TimeUnit unit) throws InterruptedException {
        while (empty) {
          unit.timedWait(this, timeout);
          ...
        }
      }
    参数:
    obj - 要等待的对象
    timeout - 要等待的最长时间。如果小于等于 0,则根本不会等待。
    抛出:
    InterruptedException - 如果等待时中断。
    另请参见:
    Object.wait(long, int)

    timedJoin

    public void timedJoin(Thread thread,
                          long timeout)
                   throws InterruptedException
    使用此时间单元执行计时的  Thread.join。这是将时间参数转换为  Thread.join 方法所需格式的便捷方法。
    参数:
    thread - 要等待的线程
    timeout - 要等待的最长时间。如果小于等于 0,则根本不会等待。
    抛出:
    InterruptedException - 如果等待时中断。
    另请参见:
    Thread.join(long, int)

    sleep

    public void sleep(long timeout)
               throws InterruptedException
    使用此单元执行  Thread.sleep.这是将时间参数转换为  Thread.sleep 方法所需格式的便捷方法。
    参数:
    timeout - 休眠的最短时间。如果小于等于 0,则根本不会休眠。
    抛出:
    InterruptedException - 如果休眠时中断。

猜你喜欢

转载自blog.csdn.net/qq_37636695/article/details/79117497
今日推荐