zoukankan      html  css  js  c++  java
  • 线程中sleep方法和wait方法有什么区别?

    如果你没有接触过java的多线程,那么多对于这两个方法可能有点陌生,看名字好像这两个方法是差不多的,但是实际上面差别好大。

    首先我们看一下官方的API

    Sleep(sleep有两个方法,另一个方法传递两个参数,还有一个参数也是时间,只不过是纳秒级别的,所以和这个方法几乎一样,所以这里只分析这个毫秒级别的方法)

    public static void sleep(long millis) throws InterruptedException

    首先是个静态的方法,入参是一个毫秒数,会抛出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.

    让当前执行的线程睡眠(临时停止执行)一段毫秒数的时间,这个时间的精确性受到系统计时器和程序调度精度影响。这个线程不会失去任何监视器的所有权。

    wait(也有多个方法,就不一一介绍了)

    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).

    让当前进程等待直到另一个其他的线程代理调用了notify() 方法或者notifyAll() 方法之后。换句话说,这个方法的行为和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() 方法。然后该线程将等到重新获得对监视器的所有权后才能继续执行。

    As in the one argument version, interrupts and spurious wakeups are possible, and this method should always be used in a loop:

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

    后面对这两个方法做个总结

    共同点
    1. 他们都是在多线程的环境下,都可以在程序的调用处阻塞指定的毫秒数,并返回。 
    2. wait()和sleep()都可以通过interrupt()方法 打断线程的暂停状态 ,从而使线程立刻抛出InterruptedException。 
    如果线程A希望立即结束线程B,则可以对线程B对应的Thread实例调用interrupt方法。如果此刻线程B正在wait/sleep/join,则线程B会立刻抛出InterruptedException,在catch() {} 中直接return即可安全地结束线程。 
    需要注意的是,InterruptedException是线程自己从内部抛出的,并不是interrupt()方法抛出的。对某一线程调用 interrupt()时,如果该线程正在执行普通的代码,那么该线程根本就不会抛出InterruptedException。但是,一旦该线程进入到 wait()/sleep()/join()后,就会立刻抛出InterruptedException 。


    不同点
    1.每个对象都有一个锁来控制同步访问。Synchronized关键字可以和对象的锁交互,来实现线程的同步。 
    sleep方法没有释放锁,而wait方法释放了锁,使得其他线程可以使用同步控制块或者方法。 
    2.wait,notify和notifyAll只能在同步控制方法或者同步控制块里面使用,而sleep可以在任何地方使用 
    3.sleep必须捕获异常,而wait,notify和notifyAll不需要捕获异常 

    4.sleep是线程类(Thread)的方法,导致此线程暂停执行指定时间,给执行机会给其他线程,但是监控状态依然保持,到时后会自动恢复。调用sleep不会释放对象锁。

    5.wait是Object类的方法,对此对象调用wait方法导致本线程放弃对象锁,进入等待此对象的等待锁定池,只有针对此对象发出notify方法(或notifyAll)后本线程才进入对象锁定池准备获得对象锁进入运行状态。

  • 相关阅读:
    北京师范大学2016考研复试分数线
    中国人民大学2016考研复试基本分数线
    厦门大学2016年硕士研究生复试基本分数线
    上海交通大学2016年硕士复试基本分数线
    swift
    swift
    swift
    iOS 开发自定义一个提示框
    swift
    swift
  • 原文地址:https://www.cnblogs.com/linkstar/p/6043846.html
Copyright © 2011-2022 走看看