枚举类的使用
java.util.concurrent.TimeUnit

使用 TimeUnit 的包
描述
提供对 Java 编程语言的设计至关重要的类。
定义通道,它表示与能够执行 I/O 操作的实体的连接,例如文件和套接字;定义选择器,用于多路复用、非阻塞 I/O 操作。
定义Java虚拟机访问文件、文件属性和文件系统的接口和类。
提供对文件和文件系统属性的访问的接口和类。
通常在并发编程中有用的实用程序类。
接口和类提供了一个框架,用于锁定和等待不同于内置同步和监视器的条件。
提供一组“轻量级”(全 Java 语言)组件,尽可能在所有平台上以相同的方式工作。
  • TimeUnitjava.lang 中的用法

    java.lang 中的方法,参数类型为 TimeUnit
    修饰符和类型
    方法
    描述
    boolean
    Process.waitFor(long timeout, TimeUnit unit)
    如有必要,使当前线程等待,直到此 Process 对象表示的进程终止,或指定的等待时间结束。
  • TimeUnitjava.nio.channels 中的用法

    java.nio.channels 中的方法,参数类型为 TimeUnit
    修饰符和类型
    方法
    描述
    abstract boolean
    AsynchronousChannelGroup.awaitTermination(long timeout, TimeUnit unit)
    等待组的终止。
    abstract <A> void
    AsynchronousSocketChannel.read(ByteBuffer[] dsts, int offset, int length, long timeout, TimeUnit unit, A attachment, CompletionHandler<Long,? super A> handler)
    从此通道读取字节序列到给定缓冲区的子序列中。
    abstract <A> void
    AsynchronousSocketChannel.read(ByteBuffer dst, long timeout, TimeUnit unit, A attachment, CompletionHandler<Integer,? super A> handler)
    从此通道读取一系列字节到给定的缓冲区中。
    abstract <A> void
    AsynchronousSocketChannel.write(ByteBuffer[] srcs, int offset, int length, long timeout, TimeUnit unit, A attachment, CompletionHandler<Long,? super A> handler)
    从给定缓冲区的子序列中将字节序列写入此通道。
    abstract <A> void
    AsynchronousSocketChannel.write(ByteBuffer src, long timeout, TimeUnit unit, A attachment, CompletionHandler<Integer,? super A> handler)
    将给定缓冲区中的字节序列写入此通道。
  • TimeUnitjava.nio.file 中的用法

    java.nio.file 中的方法,参数类型为 TimeUnit
    修饰符和类型
    方法
    描述
    WatchService.poll(long timeout, TimeUnit unit)
    检索并删除下一个监视键,如果还没有,则在必要时等待指定的等待时间。
  • TimeUnitjava.nio.file.attribute 中的用法

    java.nio.file.attribute 中的方法,参数类型为 TimeUnit
    修饰符和类型
    方法
    描述
    static FileTime
    FileTime.from(long value, TimeUnit unit)
    返回一个 FileTime 表示给定粒度单位的值。
    long
    FileTime.to(TimeUnit unit)
    返回给定粒度单位的值。
  • TimeUnitjava.util.concurrent 中的用法

    java.util.concurrent 中返回 TimeUnit 的方法
    修饰符和类型
    方法
    描述
    static TimeUnit
    TimeUnit.of(ChronoUnit chronoUnit)
    ChronoUnit 转换为等效的 TimeUnit
    static TimeUnit
    TimeUnit.valueOf(String name)
    返回具有指定名称的此类的枚举常量。
    static TimeUnit[]
    TimeUnit.values()
    返回一个数组,其中包含此枚举类的常量,按照它们声明的顺序排列。
    java.util.concurrent 中的方法,参数类型为 TimeUnit
    修饰符和类型
    方法
    描述
    boolean
    CountDownLatch.await(long timeout, TimeUnit unit)
    导致当前线程等待,直到锁存器倒计时为零,除非线程为 interrupted 或指定的等待时间已过。
    int
    CyclicBarrier.await(long timeout, TimeUnit unit)
    等待直到所有 parties 在此屏障上调用 await,或者指定的等待时间结束。
    int
    Phaser.awaitAdvanceInterruptibly(int phase, long timeout, TimeUnit unit)
    等待此移相器的相位从给定的相位值前进或给定的超时时间过去,如果在等待时被中断则抛出 InterruptedException,或者如果当前相位不等于给定的相位值或此移相器终止则立即返回。
    boolean
    ForkJoinPool.awaitQuiescence(long timeout, TimeUnit unit)
    如果由在此池中运行的 ForkJoinTask 调用,则等效于 ForkJoinTask.helpQuiesce()
    boolean
    ExecutorService.awaitTermination(long timeout, TimeUnit unit)
    阻塞直到所有任务在关闭请求后完成执行,或者发生超时,或者当前线程被中断,以先发生者为准。
    boolean
    ForkJoinPool.awaitTermination(long timeout, TimeUnit unit)
    阻塞直到所有任务在关闭请求后完成执行,或者发生超时,或者当前线程被中断,以先发生者为准。
    boolean
    ThreadPoolExecutor.awaitTermination(long timeout, TimeUnit unit)
     
    CompletableFuture.completeOnTimeout(T value, long timeout, TimeUnit unit)
    如果在给定超时之前未以其他方式完成,则使用给定值完成此 CompletableFuture。
    long
    TimeUnit.convert(long sourceDuration, TimeUnit sourceUnit)
    将给定单位的给定持续时间转换为该单位。
    static Executor
    CompletableFuture.delayedExecutor(long delay, TimeUnit unit)
    返回一个新的 Executor,它在给定的延迟(如果为非正数则没有延迟)后将任务提交给默认执行器。
    static Executor
    CompletableFuture.delayedExecutor(long delay, TimeUnit unit, Executor executor)
    返回一个新的 Executor,它在给定的延迟后(如果为非正则则不延迟)向给定的基本执行器提交任务。
    V
    Exchanger.exchange(V x, long timeout, TimeUnit unit)
    等待另一个线程到达这个交换点(除非当前线程是 interrupted 或指定的等待时间已过),然后将给定的对象传输给它,接收它的对象作为回报。
    T
    CompletableFuture.get(long timeout, TimeUnit unit)
    如有必要,最多等待此未来完成的给定时间,然后返回其结果(如果可用)。
    final V
    ForkJoinTask.get(long timeout, TimeUnit unit)
    如有必要,最多等待计算完成的给定时间,然后检索其结果(如果可用)。
    V
    Future.get(long timeout, TimeUnit unit)
    如有必要,最多等待计算完成的给定时间,然后检索其结果(如果可用)。
    V
    FutureTask.get(long timeout, TimeUnit unit)
     
    long
    Delayed.getDelay(TimeUnit unit)
    在给定时间单位内返回与此对象关联的剩余延迟。
    long
    ThreadPoolExecutor.getKeepAliveTime(TimeUnit unit)
    返回线程保持活动时间,这是线程在终止之前可以保持空闲的时间量。
    <T> List<Future<T>>
    AbstractExecutorService.invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
     
    <T> List<Future<T>>
    ExecutorService.invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
    执行给定的任务,返回一个 Futures 列表,当所有任务完成或超时到期时(以先发生者为准),保存它们的状态和结果。
    <T> T
    AbstractExecutorService.invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
     
    <T> T
    ExecutorService.invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
    执行给定的任务,返回已成功完成的任务的结果(即,没有抛出异常),如果在给定的超时结束之前有任何执行的话。
    boolean
    ArrayBlockingQueue.offer(E e, long timeout, TimeUnit unit)
    将指定的元素插入此队列的尾部,如果队列已满,则等待指定的等待时间让空间变得可用。
    boolean
    BlockingDeque.offer(E e, long timeout, TimeUnit unit)
    将指定的元素插入此双端队列表示的队列中(换句话说,在此双端队列的尾部),如有必要,等待指定的等待时间以使空间可用。
    boolean
    BlockingQueue.offer(E e, long timeout, TimeUnit unit)
    将指定的元素插入此队列,如有必要,等待指定的等待时间以获得可用空间。
    boolean
    DelayQueue.offer(E e, long timeout, TimeUnit unit)
    将指定元素插入此延迟队列。
    boolean
    LinkedBlockingDeque.offer(E e, long timeout, TimeUnit unit)
     
    boolean
    LinkedBlockingQueue.offer(E e, long timeout, TimeUnit unit)
    将指定的元素插入此队列的尾部,如有必要,等待指定的等待时间以等待空间变为可用。
    boolean
    LinkedTransferQueue.offer(E e, long timeout, TimeUnit unit)
    将指定元素插入此队列的尾部。
    boolean
    PriorityBlockingQueue.offer(E e, long timeout, TimeUnit unit)
    将指定元素插入此优先级队列。
    int
    SubmissionPublisher.offer(T item, long timeout, TimeUnit unit, BiPredicate<Flow.Subscriber<? super T>,? super T> onDrop)
    如果可能,通过异步调用其 onNext 方法将给定项发布给每个当前订阅者,在任何订阅的资源不可用时阻塞,直到指定的超时或直到调用者线程被中断,此时给定的处理程序(如果非-null) 被调用,如果它返回 true,则重试一次。
    boolean
    SynchronousQueue.offer(E e, long timeout, TimeUnit unit)
    将指定的元素插入此队列,如有必要,等待指定的等待时间以等待另一个线程接收它。
    boolean
    BlockingDeque.offerFirst(E e, long timeout, TimeUnit unit)
    将指定的元素插入此双端队列的前面,如有必要,等待指定的等待时间以使空间可用。
    boolean
    LinkedBlockingDeque.offerFirst(E e, long timeout, TimeUnit unit)
     
    boolean
    BlockingDeque.offerLast(E e, long timeout, TimeUnit unit)
    在此双端队列的末尾插入指定的元素,如有必要,等待指定的等待时间以使空间可用。
    boolean
    LinkedBlockingDeque.offerLast(E e, long timeout, TimeUnit unit)
     
    CompletableFuture.orTimeout(long timeout, TimeUnit unit)
    如果在给定超时之前未以其他方式完成,则使用 TimeoutException 异常完成此 CompletableFuture。
    E
    ArrayBlockingQueue.poll(long timeout, TimeUnit unit)
     
    E
    BlockingDeque.poll(long timeout, TimeUnit unit)
    检索并删除此双端队列所表示的队列的头部(换句话说,此双端队列的第一个元素),如有必要,等待指定的等待时间以使元素可用。
    E
    BlockingQueue.poll(long timeout, TimeUnit unit)
    检索并删除此队列的头部,如有必要,等待指定的等待时间以使元素可用。
    CompletionService.poll(long timeout, TimeUnit unit)
    检索并删除表示下一个已完成任务的 Future,必要时等待指定的等待时间(如果尚不存在)。
    E
    DelayQueue.poll(long timeout, TimeUnit unit)
    检索并移除此队列的头部,如有必要,等待直到此队列上有一个具有过期延迟的元素可用,或者指定的等待时间到期。
    ExecutorCompletionService.poll(long timeout, TimeUnit unit)
     
    E
    LinkedBlockingDeque.poll(long timeout, TimeUnit unit)
     
    E
    LinkedBlockingQueue.poll(long timeout, TimeUnit unit)
     
    E
    LinkedTransferQueue.poll(long timeout, TimeUnit unit)
     
    E
    PriorityBlockingQueue.poll(long timeout, TimeUnit unit)
     
    E
    SynchronousQueue.poll(long timeout, TimeUnit unit)
    检索并删除此队列的头部,必要时等待指定的等待时间,以等待另一个线程插入它。
    E
    BlockingDeque.pollFirst(long timeout, TimeUnit unit)
    检索并删除此双端队列的第一个元素,如有必要,等待指定的等待时间以使元素可用。
    E
    LinkedBlockingDeque.pollFirst(long timeout, TimeUnit unit)
     
    E
    BlockingDeque.pollLast(long timeout, TimeUnit unit)
    检索并删除此双端队列的最后一个元素,如有必要,等待指定的等待时间以使元素可用。
    E
    LinkedBlockingDeque.pollLast(long timeout, TimeUnit unit)
     
    final boolean
    ForkJoinTask.quietlyJoin(long timeout, TimeUnit unit)
    尝试加入此任务,如果它在给定超时之前完成(可能是异常情况)并且当前线程未被中断,则返回 true。
    final boolean
    ForkJoinTask.quietlyJoinUninterruptibly(long timeout, TimeUnit unit)
    尝试加入此任务,如果它在给定超时之前完成(可能异常),则返回 true。
    ScheduledExecutorService.schedule(Runnable command, long delay, TimeUnit unit)
    提交在给定延迟后启用的一次性任务。
    ScheduledExecutorService.schedule(Callable<V> callable, long delay, TimeUnit unit)
    提交在给定延迟后启用的返回值的一次性任务。
    ScheduledThreadPoolExecutor.schedule(Runnable command, long delay, TimeUnit unit)
     
    ScheduledThreadPoolExecutor.schedule(Callable<V> callable, long delay, TimeUnit unit)
     
    ScheduledExecutorService.scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)
    提交一个周期性动作,该动作在给定的初始延迟后首先启用,然后在给定的周期内启用;也就是说,执行将在 initialDelay 之后开始,然后是 initialDelay + period ,然后是 initialDelay + 2 * period ,依此类推。
    ScheduledThreadPoolExecutor.scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)
    提交一个周期性动作,该动作在给定的初始延迟后首先启用,然后在给定的周期内启用;也就是说,执行将在 initialDelay 之后开始,然后是 initialDelay + period ,然后是 initialDelay + 2 * period ,依此类推。
    ScheduledExecutorService.scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)
    提交一个周期性动作,该动作在给定的初始延迟后首先启用,然后在一次执行的终止和下一次执行的开始之间有给定的延迟。
    ScheduledThreadPoolExecutor.scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)
    提交一个周期性动作,该动作在给定的初始延迟后首先启用,然后在一次执行的终止和下一次执行的开始之间有给定的延迟。
    void
    ThreadPoolExecutor.setKeepAliveTime(long time, TimeUnit unit)
    设置线程保持活动时间,这是线程在终止之前可以保持空闲的时间量。
    boolean
    Semaphore.tryAcquire(int permits, long timeout, TimeUnit unit)
    如果在给定的等待时间内所有许可都可用并且当前线程尚未 interrupted ,则从该信号量获取给定数量的许可。
    boolean
    Semaphore.tryAcquire(long timeout, TimeUnit unit)
    如果信号量在给定的等待时间内变得可用并且当前线程尚未 interrupted ,则从该信号量获取许可。
    boolean
    LinkedTransferQueue.tryTransfer(E e, long timeout, TimeUnit unit)
    如果可以在超时结束前将元素传输给消费者。
    boolean
    TransferQueue.tryTransfer(E e, long timeout, TimeUnit unit)
    如果可以在超时结束前将元素传输给消费者。
    java.util.concurrent 中的构造函数,参数类型为 TimeUnit
    修饰符
    构造方法
    描述
     
    ForkJoinPool(int parallelism, ForkJoinPool.ForkJoinWorkerThreadFactory factory, Thread.UncaughtExceptionHandler handler, boolean asyncMode, int corePoolSize, int maximumPoolSize, int minimumRunnable, Predicate<? super ForkJoinPool> saturate, long keepAliveTime, TimeUnit unit)
    使用给定的参数创建一个 ForkJoinPool
     
    ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue)
    使用给定的初始参数、默认线程工厂默认拒绝执行处理程序 创建一个新的 ThreadPoolExecutor
     
    ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler)
    使用给定的初始参数和 默认线程工厂 创建一个新的 ThreadPoolExecutor
     
    ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory)
    使用给定的初始参数和 默认拒绝执行处理程序 创建一个新的 ThreadPoolExecutor
     
    ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler)
    使用给定的初始参数创建一个新的 ThreadPoolExecutor
  • TimeUnitjava.util.concurrent.locks 中的用法

    java.util.concurrent.locks 中的方法,参数类型为 TimeUnit
    修饰符和类型
    方法
    描述
    final boolean
    AbstractQueuedLongSynchronizer.ConditionObject.await(long time, TimeUnit unit)
    实现定时条件等待。
    final boolean
    AbstractQueuedSynchronizer.ConditionObject.await(long time, TimeUnit unit)
    实现定时条件等待。
    boolean
    Condition.await(long time, TimeUnit unit)
    使当前线程等待,直到收到信号或中断,或者指定的等待时间结束。
    boolean
    Lock.tryLock(long time, TimeUnit unit)
    如果在给定的等待时间内是空闲的并且当前线程还没有interrupted,则获取锁。
    boolean
    ReentrantLock.tryLock(long timeout, TimeUnit unit)
    如果在给定的等待时间内没有被另一个线程持有并且当前线程还没有被 interrupted ,则获取锁。
    boolean
    ReentrantReadWriteLock.ReadLock.tryLock(long timeout, TimeUnit unit)
    如果写锁在给定的等待时间内没有被另一个线程持有并且当前线程还没有interrupted,则获取读锁。
    boolean
    ReentrantReadWriteLock.WriteLock.tryLock(long timeout, TimeUnit unit)
    如果在给定的等待时间内没有被另一个线程持有并且当前线程还没有interrupted,则获取写锁。
    long
    StampedLock.tryReadLock(long time, TimeUnit unit)
    如果在给定时间内可用且当前线程未被中断,则非独占获取锁。
    long
    StampedLock.tryWriteLock(long time, TimeUnit unit)
    如果在给定时间内可用且当前线程未被中断,则独占获取锁。
  • TimeUnitjavax.swing 中的用法

    javax.swing 中的方法,参数类型为 TimeUnit
    修饰符和类型
    方法
    描述
    final T
    SwingWorker.get(long timeout, TimeUnit unit)
    如有必要,最多等待计算完成的给定时间,然后检索其结果(如果可用)。