java多线程之yield,join,wait,sleep的区别

Java多线程之yield,join,wait,sleep的区别

Java多线程中,经常会遇到yield,join,wait和sleep方法。容易混淆他们的功能及作用。自己仔细研究了下,他们主要的区别是在cpu的占用和共享资源的锁上面。

wait:是继承自Object的方法,当前线程调用wait方法,是在告诉别的线程,我需要等待了,既会释放cpu,也会释放共享资源的锁,进入挂起状态。wait必须在synchronized代码块内部执行,因为wait需要获得共享资源的锁并且释放锁。需要notify/notifyAll来唤醒。wait主要和sleep进行区别,

join:Thread的非静态方法。底层用了wait方法。假如现在有两个线程,main线程和t线程.main线程里调用t.join.那么这时,main会取得线程对象t的锁,然后main线程 wait,释放cpu,t线程执行,

直到t线程执行完后,main线程继续执行。

jdk中join的源代码:

    /**
* 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 {
long base = System.currentTimeMillis();
long now = 0; if (millis < 0) {
throw new IllegalArgumentException("timeout value is negative");
} if (millis == 0) {
while (isAlive()) {
wait(0);
}
} else {
while (isAlive()) {
long delay = millis - now;
if (delay <= 0) {
break;
}
wait(delay);
now = System.currentTimeMillis() - base;
}
}
} /**
* Tests if this thread is alive. A thread is alive if it has
* been started and has not yet died.
*
* @return <code>true</code> if this thread is alive;
* <code>false</code> otherwise.
*/
public final native boolean isAlive();

通过源代码,可以看出,如果t线程生成了,但是还未start,那么join方法是没有效果的。

sleep:是Thread的静态方法,会使当前线程释放cpu,但不会释放锁资源,可以理解为只和cpu有关,不涉及锁资源。涉及锁资源的,是wait,join方法。

yield:也是Thread的静态方法,和sleep方法类似,会使当前线程释放cpu,但不会释放锁资源。和sleep不同的是,sleep必须设置时间,但是yield不能设置时间,时间值是随机的

上一篇:JAVA多线程之wait/notify


下一篇:JAVA多线程之UncaughtExceptionHandler——处理非正常的线程中止