Print this page


Split Close
Expand all
Collapse all
          --- old/src/share/classes/java/util/concurrent/TimeUnit.java
          +++ new/src/share/classes/java/util/concurrent/TimeUnit.java
↓ open down ↓ 45 lines elided ↑ open up ↑
  46   46   * millisecond, a millisecond as one thousandth of a second, a minute
  47   47   * as sixty seconds, an hour as sixty minutes, and a day as twenty four
  48   48   * hours.
  49   49   *
  50   50   * <p>A <tt>TimeUnit</tt> is mainly used to inform time-based methods
  51   51   * how a given timing parameter should be interpreted. For example,
  52   52   * the following code will timeout in 50 milliseconds if the {@link
  53   53   * java.util.concurrent.locks.Lock lock} is not available:
  54   54   *
  55   55   * <pre>  Lock lock = ...;
  56      - *  if ( lock.tryLock(50L, TimeUnit.MILLISECONDS) ) ...
       56 + *  if (lock.tryLock(50L, TimeUnit.MILLISECONDS)) ...
  57   57   * </pre>
  58   58   * while this code will timeout in 50 seconds:
  59   59   * <pre>
  60   60   *  Lock lock = ...;
  61      - *  if ( lock.tryLock(50L, TimeUnit.SECONDS) ) ...
       61 + *  if (lock.tryLock(50L, TimeUnit.SECONDS)) ...
  62   62   * </pre>
  63   63   *
  64   64   * Note however, that there is no guarantee that a particular timeout
  65   65   * implementation will be able to notice the passage of time at the
  66   66   * same granularity as the given <tt>TimeUnit</tt>.
  67   67   *
  68   68   * @since 1.5
  69   69   * @author Doug Lea
  70   70   */
  71   71  public enum TimeUnit {
↓ open down ↓ 212 lines elided ↑ open up ↑
 284  284      /**
 285  285       * Utility to compute the excess-nanosecond argument to wait,
 286  286       * sleep, join.
 287  287       * @param d the duration
 288  288       * @param m the number of milliseconds
 289  289       * @return the number of nanoseconds
 290  290       */
 291  291      abstract int excessNanos(long d, long m);
 292  292  
 293  293      /**
 294      -     * Performs a timed <tt>Object.wait</tt> using this time unit.
      294 +     * Performs a timed {@link Object#wait(long, int) Object.wait}
      295 +     * using this time unit.
 295  296       * This is a convenience method that converts timeout arguments
 296  297       * into the form required by the <tt>Object.wait</tt> method.
 297  298       *
 298  299       * <p>For example, you could implement a blocking <tt>poll</tt>
 299  300       * method (see {@link BlockingQueue#poll BlockingQueue.poll})
 300  301       * using:
 301  302       *
 302      -     * <pre>  public synchronized Object poll(long timeout, TimeUnit unit) throws InterruptedException {
 303      -     *    while (empty) {
 304      -     *      unit.timedWait(this, timeout);
 305      -     *      ...
 306      -     *    }
 307      -     *  }</pre>
      303 +     *  <pre> {@code
      304 +     * public synchronized Object poll(long timeout, TimeUnit unit)
      305 +     *     throws InterruptedException {
      306 +     *   while (empty) {
      307 +     *     unit.timedWait(this, timeout);
      308 +     *     ...
      309 +     *   }
      310 +     * }}</pre>
 308  311       *
 309  312       * @param obj the object to wait on
 310  313       * @param timeout the maximum time to wait. If less than
 311  314       * or equal to zero, do not wait at all.
 312      -     * @throws InterruptedException if interrupted while waiting.
 313      -     * @see Object#wait(long, int)
      315 +     * @throws InterruptedException if interrupted while waiting
 314  316       */
 315  317      public void timedWait(Object obj, long timeout)
 316      -    throws InterruptedException {
      318 +            throws InterruptedException {
 317  319          if (timeout > 0) {
 318  320              long ms = toMillis(timeout);
 319  321              int ns = excessNanos(timeout, ms);
 320  322              obj.wait(ms, ns);
 321  323          }
 322  324      }
 323  325  
 324  326      /**
 325      -     * Performs a timed <tt>Thread.join</tt> using this time unit.
      327 +     * Performs a timed {@link Thread#join(long, int) Thread.join}
      328 +     * using this time unit.
 326  329       * This is a convenience method that converts time arguments into the
 327  330       * form required by the <tt>Thread.join</tt> method.
      331 +     *
 328  332       * @param thread the thread to wait for
 329  333       * @param timeout the maximum time to wait. If less than
 330  334       * or equal to zero, do not wait at all.
 331      -     * @throws InterruptedException if interrupted while waiting.
 332      -     * @see Thread#join(long, int)
      335 +     * @throws InterruptedException if interrupted while waiting
 333  336       */
 334  337      public void timedJoin(Thread thread, long timeout)
 335      -    throws InterruptedException {
      338 +            throws InterruptedException {
 336  339          if (timeout > 0) {
 337  340              long ms = toMillis(timeout);
 338  341              int ns = excessNanos(timeout, ms);
 339  342              thread.join(ms, ns);
 340  343          }
 341  344      }
 342  345  
 343  346      /**
 344      -     * Performs a <tt>Thread.sleep</tt> using this unit.
      347 +     * Performs a {@link Thread#sleep(long, int) Thread.sleep} using
      348 +     * this time unit.
 345  349       * This is a convenience method that converts time arguments into the
 346  350       * form required by the <tt>Thread.sleep</tt> method.
      351 +     *
 347  352       * @param timeout the minimum time to sleep. If less than
 348  353       * or equal to zero, do not sleep at all.
 349      -     * @throws InterruptedException if interrupted while sleeping.
 350      -     * @see Thread#sleep
      354 +     * @throws InterruptedException if interrupted while sleeping
 351  355       */
 352  356      public void sleep(long timeout) throws InterruptedException {
 353  357          if (timeout > 0) {
 354  358              long ms = toMillis(timeout);
 355  359              int ns = excessNanos(timeout, ms);
 356  360              Thread.sleep(ms, ns);
 357  361          }
 358  362      }
 359  363  
 360  364  }
    
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX