1

1、wait的用法

/**
 * Causes the current thread to wait until another thread invokes the * {@link java.lang.Object#notify()} method or the
 * {@link java.lang.Object#notifyAll()} method for this object.
 * In other words, this method behaves exactly as if it simply * performs the call {@code wait(0)}.
 * <p>
 * The current thread must own this object's monitor. The thread
 * releases ownership of this monitor and waits until another thread * notifies threads waiting on this object's monitor to wake up * either through a call to the {@code notify} method or the
 * {@code notifyAll} method. The thread then waits until it can
 * re-obtain ownership of the monitor and resumes execution. * <p>
 * As in the one argument version, interrupts and spurious wakeups are
 * possible, and this method should always be used in a loop: * <pre>
 *     synchronized (obj) {
 *         while (&lt;condition does not hold&gt;)
 *             obj.wait(); *         ... // Perform action appropriate to condition *     } * </pre>
 * This method should only be called by a thread that is the owner
 * of this object's monitor. See the {@code notify} method for a
 * description of the ways in which a thread can become the owner of * a monitor. * * @throws IllegalMonitorStateException  if the current thread is not
 *               the owner of the object's monitor. * @throws InterruptedException if any thread interrupted the
 *             current thread before or while the current thread *             was waiting for a notification.  The <i>interrupted
 *             status</i> of the current thread is cleared when
 *             this exception is thrown. * @see java.lang.Object#notify()
 * @see java.lang.Object#notifyAll()
 */
 public final void wait() throws InterruptedException {
    wait(0);
}
  • 参考wait的注解, 当调用wait的时候必须要持有监视器锁,不然会跑出非法监视器异常

image.png
为什么wait()和notify()需要搭配synchonized关键字使用

2 sleep

/**
 * Causes the currently executing thread to sleep (temporarily cease * execution) for the specified number of milliseconds, subject to * the precision and accuracy of system timers and schedulers. The thread * does not lose ownership of any monitors. * * @param millis
 *         the length of time to sleep in milliseconds
 * * @throws IllegalArgumentException
 *          if the value of {@code millis} is negative
 * * @throws InterruptedException
 *          if any thread has interrupted the current thread. The * <i>interrupted status</i> of the current thread is
 *          cleared when this exception is thrown. 
 */
 public static native void sleep(long millis) throws InterruptedException;

3 Semaphore

/**
 * A counting semaphore.  Conceptually, a semaphore maintains a set of * permits.  Each {@link #acquire} blocks if necessary until a permit is
 * available, and then takes it.  Each {@link #release} adds a permit,
 * potentially releasing a blocking acquirer. * However, no actual permit objects are used; the {@code Semaphore} just
 * keeps a count of the number available and acts accordingly. 
 */
/**
 * Acquires a permit from this semaphore, blocking until one is * available, or the thread is {@linkplain Thread#interrupt interrupted}.
 * * <p>Acquires a permit, if one is available and returns immediately,
 * reducing the number of available permits by one. * * <p>If no permit is available then the current thread becomes
 * disabled for thread scheduling purposes and lies dormant until * one of two things happens: * <ul>
 * <li>Some other thread invokes the {@link #release} method for this
 * semaphore and the current thread is next to be assigned a permit; or * <li>Some other thread {@linkplain Thread#interrupt interrupts}
 * the current thread. * </ul>
 *
 * <p>If the current thread:
 * <ul>
 * <li>has its interrupted status set on entry to this method; or
 * <li>is {@linkplain Thread#interrupt interrupted} while waiting
 * for a permit, * </ul>
 * then {@link InterruptedException} is thrown and the current thread's
 * interrupted status is cleared. * * @throws InterruptedException if the current thread is interrupted
 */
 public void acquire() throws InterruptedException {
    sync.acquireSharedInterruptibly(1);
}
/**
 * Releases a permit, returning it to the semaphore. * * <p>Releases a permit, increasing the number of available permits by
 * one.  If any threads are trying to acquire a permit, then one is * selected and given the permit that was just released.  That thread * is (re)enabled for thread scheduling purposes. * * <p>There is no requirement that a thread that releases a permit must
 * have acquired that permit by calling {@link #acquire}.
 * Correct usage of a semaphore is established by programming convention * in the application. 
 */
 public void release() {
    sync.releaseShared(1);
}

沂狰码农
16 声望1 粉丝

CODE BETTER