zoukankan      html  css  js  c++  java
  • 多线程wait()和sleep()以及InterruptedException异常

    1.核心区别
    sleep用于线程控制,wait用于线程间的通信。
    sleep是Thread类的方法,是让线程休息一段时间,然后自动恢复运行,与其他线程无关,与同步无关,也与锁无关(拿锁时不会释放锁)。
    wait是Object类的方法,也就是锁的方法,必然在同步中,根据java7.0的解释(The thread releases ownership of this monitor and waits),wait就是让当前线程释放锁并等待。(java 1.6中文版API翻译是错误的,“该线程发布对此监视器的所有权并等待”,release应该翻译成释放)。
    public static void sleep(long millis)
                      throws InterruptedException
    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.
    在指定的毫秒数内让当前正在执行的线程休眠(暂停执行),此操作受到系统计时器和调度程序精度和准确性的影响。该线程不丢失任何监视器的所属权。
    public final void wait()
                    throws InterruptedException
    Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object. In other words, this method behaves exactly as if it simply performs the call wait(0).
    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 notify method or the notifyAll method. The thread then waits until it can re-obtain ownership of the monitor and resumes execution.
    在其他线程调用此对象的 notify() 方法或 notifyAll() 方法前,导致当前线程等待。换句话说,此方法的行为就好像它仅执行 wait(0) 调用一样。 
    当前线程必须拥有此对象监视器。该线程发布对此监视器的所有权并等待,直到其他线程通过调用 notify 方法,或 notifyAll 方法通知在此对象的监视器上等待的线程醒来。然后该线程将等到重新获得对监视器的所有权后才能继续执行。 
     
    2.唤醒和中断
    sleep一觉睡到自然醒,wait就必须其他拿锁的线程调用notify(唤醒一个)或notifyAll(全部唤醒)方法来唤醒。wait的线程被唤醒后,不会立即执行,它要和其他具备执行资格的线程正常的争抢执行权。
    由于sleep和wait会使线程处于阻塞或冻结状态,可能等不到所等的事件而无法终止,所以需要从外部调用interrupt()方法中断他们的阻塞状态,此时就会抛InterruptedException异常(API对这个异常的描述为:当线程在活动之前或活动期间处于正在等待、休眠或占用状态且该线程被中断时,抛出该异常。
  • 相关阅读:
    poj 3636
    poj 1065 Wooden Sticks
    2017北京国庆刷题Day6 afternoon
    2017北京国庆刷题Day5 afternoon
    2017北京国庆刷题Day3 afternoon
    2017北京国庆刷题Day3 morning
    2017北京国庆刷题Day2 afternoon
    2017北京国庆刷题Day2 morning
    poj2420 A Star not a Tree?
    NOIP2016 愤怒的小鸟
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/4935715.html
Copyright © 2011-2022 走看看