好了、說了多線程,那就不得不說說多線程的sleep()、join()和yield()三個方法的區(qū)別啦
? 1、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. *
意思是說:當前正在執(zhí)行的線程休眠(暫時停止執(zhí)行)指定的毫秒數(shù),具體取決于系統(tǒng)計時器和調(diào)度程序的精度和準確性。 該線程不會失去任何監(jiān)視器的所有權(quán)。 *@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;
其實主要的就是他是讓其他線程走,自己進行休眠,但是自己卻不會釋放對象鎖,也就是說,如果有同步鎖的時候,其他線程不能訪問共享數(shù)據(jù)。
注意該方法要捕獲異常 比如有兩個線程同時執(zhí)行(沒有Synchronized),一個線程優(yōu)先級為MAX_PRIORITY,另一
個為MIN_PRIORITY,如果沒有Sleep()方法,只有高優(yōu)先級的線程執(zhí)行完成后,低優(yōu)先級
的線程才能執(zhí)行;但當高優(yōu)先級的線程sleep(5000)后,低優(yōu)先級就有機會執(zhí)行了。
總之,sleep()可以使低優(yōu)先級的線程得到執(zhí)行的機會,當然也可以讓同優(yōu)先級、高優(yōu)先級的 線程有執(zhí)行的機會。
2、yield() 方法
/** * A hint to the scheduler that the current thread is willing to yield *
its current use of a processor. The scheduler is free to ignore this * hint. *
意思是說 提示當前線程可以讓處理器忽略當前線程,去處理其他線程 * <p> Yield is a heuristic attempt to improve
relative progression * between threads that would otherwise over-utilise a CPU.
Its use * should be combined with detailed profiling and benchmarking to *
ensure that it actually has the desired effect. *
它是一種啟發(fā)式嘗試,用于改善線程之間的相對進展,否則會過度利用CPU。 它的使用應與詳細的分析和基準測試相結(jié)合,以確保它實際上具有所需的效果。 * <p>
It is rarely appropriate to use this method. It may be useful * for debugging
or testing purposes, where it may help to reproduce * bugs due to race
conditions. It may also be useful when designing * concurrency control
constructs such as the ones in the * {@link java.util.concurrent.locks} package.
* 使用這種方法很少是合適的。 它可能對調(diào)試或測試目的很有用,它可能有助于重現(xiàn)因競爭條件而產(chǎn)生的錯誤。
在設計并發(fā)控制結(jié)構(gòu)(如中的那些)時,它也可能很有用*/ public static native void yield();
yield() 這個方法從以上注釋可以看出,也是一個休眠自身線程的方法,同樣不會釋放自身鎖的標識,區(qū)別在于它是沒有參數(shù)的,即yield()方
法只是使當前線程重新回到可執(zhí)行狀態(tài),
所以執(zhí)行yield()的線程有可能在進入到可執(zhí)行狀態(tài) 后馬上又被執(zhí)行,另外yield()方法只能使同優(yōu)先級或者高優(yōu)先級的線程得到執(zhí)行機會,這也
和sleep()方法不同。
?
3、join() 方法
這個方法比較有意思,Thread的非靜態(tài)方法join()讓一個線程B“加入”到另外一個線程A的尾部。在A執(zhí)行完畢之前, B不能工作。
/** * Waits for this thread to die. * 等待線程死亡 * <p> An invocation of this
method behaves in exactly the same * way as the invocation * * <blockquote> * {
@linkplain #join(long) join}{@code (0)} * </blockquote> * * @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 final void join() throws InterruptedException {
join(0); // 調(diào)用了有參方法 } /** * Waits at most {@code millis} milliseconds for this
thread to * die. A timeout of {@code 0} means to wait forever. *
這個線程最多等多少毫秒,如果超時了,就會進行線程死鎖 * <p> This implementation uses a loop of {@code
this.wait} calls * conditioned on {@code this.isAlive}. As a thread terminates
the * {@code this.notifyAll} method is invoked. It is recommended that *
applications not use {@code wait}, {@code notify}, or * {@code notifyAll} on {
@code Thread} instances. * * @param millis * the time to wait 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 final synchronized void join(long millis)
throws InterruptedException {
保證當前線程停止執(zhí)行,直到該線程所加入的線程完成為止。然而,如果它加入的線程沒有存活,則當前線程不需要停止。
?
熱門工具 換一換