youyichannel

志于道,据于德,依于仁,游于艺!

0%

JUC初探-06

Thread#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;

1)是否释放锁

从注释中可以看出sleep的线程不会释放锁The thread does not lose ownership of any monitors.

2)是否对中断敏感

注释中可以看出响应中断if any thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.

3)是否释放CPU

代码示例:

public class SleepReleaseCPU {

public static void main(String[] args) {
for (int i = 1; i <= 100; i++) {
new Thread(new SubThread(), "Thread - " + i).start();
}
}

static class SubThread implements Runnable {
@Override
public void run() {
try {
System.out.println(Thread.currentThread().getName());
Thread.sleep(50000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally {
System.out.println("FINISH!");
}
}
}
}

运行中,CPU压力没有明显提升,说明sleep的线程会释放CPU,线程处于TIMED_WAITING状态,进入等待队列。

Object#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);
}

1)是否释放锁

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 notify method or the notifyAll method.

2)是否对中断敏感

if any thread interrupted the current thread before or while the current thread was waiting for a notification. The interrupted status of the current thread is cleared when this exception is thrown.

3)是否释放CPU

,wait的线程让出CPU时间片,进入等待队列。

资料:https://www.bilibili.com/video/BV1Hw411p7Qk/ 强推 河北王校长