更新時間:2018-03-12
大家都知道sleep方法是Thread中的驯用,wait方法來自于Object脸秽。但是調(diào)用了sleep和wait方法以后,線程處于什么狀態(tài)蝴乔?這里记餐,我們從jdk源碼開始分析。版本是oracle jdk 1.7薇正。
這里是sleep方法的源碼:上面有一句:The thread does not lose ownership of any monitors.翻譯過來就是不會放棄monitor對象,即不會放棄鎖挖腰;
/**
* 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;
這里看到的是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 (<condition does not hold>)
* 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.
*
* @exception IllegalMonitorStateException if the current thread is not
* the owner of the object's monitor.
* @exception 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);
}
然后我們再看看Thread都有哪些狀態(tài)呢猴仑?NEW, RUNNABLE,BLOCKED,WAITING,TIMED_WAITING,TERMINATED;
一共6個狀態(tài)审轮,可以明顯看到,當(dāng)代碼塊遇到synchronize修飾時會轉(zhuǎn)換成狀態(tài):BLOCKED。當(dāng)調(diào)用wait()與join()方法時會變成狀態(tài):WAITING断国;當(dāng)調(diào)用wait(long)贤姆、join(long)、Thread.sleep方法時狀態(tài)為:TIMED_WAITING稳衬。
public enum State {
/**
* Thread state for a thread which has not yet started.
*/
NEW,
/**
* Thread state for a runnable thread. A thread in the runnable
* state is executing in the Java virtual machine but it may
* be waiting for other resources from the operating system
* such as processor.
*/
RUNNABLE,
/**
* Thread state for a thread blocked waiting for a monitor lock.
* A thread in the blocked state is waiting for a monitor lock
* to enter a synchronized block/method or
* reenter a synchronized block/method after calling
* {@link Object#wait() Object.wait}.
*/
BLOCKED,
/**
* Thread state for a waiting thread.
* A thread is in the waiting state due to calling one of the
* following methods:
* <ul>
* <li>{@link Object#wait() Object.wait} with no timeout</li>
* <li>{@link #join() Thread.join} with no timeout</li>
* <li>{@link LockSupport#park() LockSupport.park}</li>
* </ul>
*
* <p>A thread in the waiting state is waiting for another thread to
* perform a particular action.
*
* For example, a thread that has called <tt>Object.wait()</tt>
* on an object is waiting for another thread to call
* <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on
* that object. A thread that has called <tt>Thread.join()</tt>
* is waiting for a specified thread to terminate.
*/
WAITING,
/**
* Thread state for a waiting thread with a specified waiting time.
* A thread is in the timed waiting state due to calling one of
* the following methods with a specified positive waiting time:
* <ul>
* <li>{@link #sleep Thread.sleep}</li>
* <li>{@link Object#wait(long) Object.wait} with timeout</li>
* <li>{@link #join(long) Thread.join} with timeout</li>
* <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li>
* <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li>
* </ul>
*/
TIMED_WAITING,
/**
* Thread state for a terminated thread.
* The thread has completed execution.
*/
TERMINATED;
}
所以霞捡,sleep和wait方法有什么區(qū)別呢?總結(jié)下:
1.sleep方法是Thread中的薄疚,wait方法來自于Object碧信;
2.當(dāng)調(diào)用wait()方法時進(jìn)入WAITING狀態(tài),調(diào)用wait(long)和sleep方法時街夭,都可以進(jìn)入TIMED_WAITING狀態(tài)砰碴;
3.wait調(diào)用后需要被其他線程喚醒(notify/notifyAll);
4.sleep方法調(diào)用后不會放棄cpu資源板丽,而wait方法放棄鎖并在下次得到cpu資源才能繼續(xù)運(yùn)行呈枉;