Java中wait sleep await 区别于联系

时间:2018-09-15 20:33:33   收藏:0   阅读:372

前言:本文解决的问题

在找工作的各种笔试题目中,经常看到wait()、sleep()还有await(),功能都很相似,到底有什么区别?什么时候该用哪一种方法

1. wait() VS sleep()

wait和sleep的比较可以说是高频面试题。方法原型分别为:

 public final native void wait(long timeout) throws InterruptedException;

 public static native void sleep(long millis) throws InterruptedException;

同:

异:

2. wait() VS await()

这两个长得很像。await()的实现比较复杂。

  public final void await() throws InterruptedException {
            if (Thread.interrupted())
                throw new InterruptedException();
            Node node = addConditionWaiter();
            int savedState = fullyRelease(node);
            int interruptMode = 0;
            while (!isOnSyncQueue(node)) {
                LockSupport.park(this);
                if ((interruptMode = checkInterruptWhileWaiting(node)) != 0)
                    break;
            }
            if (acquireQueued(node, savedState) && interruptMode != THROW_IE)
                interruptMode = REINTERRUPT;
            if (node.nextWaiter != null) // clean up if cancelled
                unlinkCancelledWaiters();
            if (interruptMode != 0)
                reportInterruptAfterWait(interruptMode);
        }

先说下来源,await是ConditionObject类里面的方法,ConditionObject实现了Condition接口;而ReentrantLock里面默认有实现newCondition()方法,新建一个条件对象。该方法就是用在ReentrantLock中根据条件来设置等待。唤醒方法也是由专门的Signal()或者Signal()来执行。另外await会导致当前线程被阻塞,会放弃锁,这点和wait是一样的。

由于所在的超类不同使用场景也不同,wait一般用于Synchronized中,而await只能用于ReentrantLock锁中,具体如下
wait()

          synchronized (obj) {
              while (<condition does not hold>)
               obj.wait(timeout);
             ... // Perform action appropriate to condition
         }

await()主要见上文。

3 notify signal

顺便说下这二者的区别,notify使用来唤醒使用wait的线程;而signal是用来唤醒await线程。

原文:https://www.cnblogs.com/java-learner/p/9652027.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!