Sleep与wait的区别

  1、这两个方法来自不同的类分别是Thread和Object

  2、最主要是sleep方法没有释放锁,而wait方法释放了锁,使得其他线程可以使用同步控制块或者方法。

  3、wait,notify和notifyAll只能在同步控制方法或者同步控制块里面使用,而sleep可以在

  任何地方使用(使用范围)

  synchronized(x){

      x.notify()

      //或者wait()

  }

  4、sleep必须捕获异常,而wait,notify和notifyAll不需要捕获异常

  

        扩充阅读:

  java 线程中的sleep和wait有一个共同作用,停止当前线程任务运行,但他们存在一定的不同,首先我们先看sleep中的构造函数

  sleep(long millis)           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.

  sleep(long millis, int nanos)          Causes the currently executing thread to sleep (cease execution) for the specified number of milliseconds plus the specified number of nanoseconds, subject to the precision and accuracy of system timers and schedulers.

  sleep方法属于Thread类中方法,表示让一个线程进入睡眠状态,等待一定的时间之后,自动醒来进入到可运行状态,不会马上进入运行状态,因为线程调度机制恢复线程的运行也需要时间,一个线程对象调用了sleep方法之后,并不会释放他所持有的所有对象锁,所以也就不会影响其他进程对象的运行。但在sleep的过程中过程中有可能被其他对象调用它的interrupt(),产生InterruptedException异常,如果你的程序不捕获这个异常,线程就会异常终止,进入TERMINATED状态,如果你的程序捕获了这个异常,那么程序就会继续执行catch语句块(可能还有finally语句块)以及以后的代码。

  注意sleep()方法是一个静态方法,也就是说他只对当前对象有效,通过t.sleep()让t对象进入sleep,这样的做法是错误的,它只会是使当前线程被sleep 而不是t线程

  wait方法

  void wait(long timeout)

  Causes the current thread to wait until either another thread invokes the notify() method or the notifyAll() method for this object, or a specified amount of time has elapsed.

  void wait(long timeout, int nanos)

  Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object, or some other thread interrupts the current thread, or a certain amount of real time has elapsed.

  wait属于Object的成员方法,一旦一个对象调用了wait方法,必须要采用notify()和notifyAll()方法唤醒该进程;如果线程拥有某个或某些对象的同步锁,那么在调用了wait()后,这个线程就会释放它持有的所有同步资源,而不限于这个被调用了wait()方法的对象。wait()方法也同样会在wait的过程中有可能被其他对象调用interrupt()方法而产生

  InterruptedException,效果以及处理方式同sleep()方法

 

  sleep()方法和wait()方法都成产生让当前运行的线程停止运行的效果,这是它们的共同点。下面我们来详细说说它们的不同之处。 

  sleep()方法是本地方法,属于Thread类,它有两种定义:
 
  1. public static native void sleep(long millis) throws InterruptedException;  
  2. public static void sleep(long millis, int nanos) throws InterruptedException {  
  3.     //other code  

        其中的参数millis代表毫秒数(千分之一秒),nanos代表纳秒数(十亿分之一秒)。这两个方法都可以让调用它的线程沉睡(停止运行)指定的时间,到了这个时间,线程就会自动醒来,变为可运行状态(RUNNABLE),但这并不表示它马上就会被运行,因为线程调度机制恢复线程的运行也需要时间。调用sleep()方法并不会让线程释放它所持有的同步锁;而且在这期间它也不会阻碍其它线程的运行。上面的连个方法都声明抛出一个InterruptedException类型的异常,这是因为线程在sleep()期间,有可能被持有它的引用的其它线程调用它的interrupt()方法而中断。中断一个线程会导致一个InterruptedException异常的产生,如果你的程序不捕获这个异常,线程就会异常终止,进入TERMINATED状态,如果你的程序捕获了这个异常,那么程序就会继续执行catch语句块(可能还有finally语句块)以及以后的代码。 

        为了更好地理解interrupt()效果,我们来看一下下面这个例子:
 

  1. public class InterruptTest {  
  2.     public static void main(String[] args) {  
  3.         Thread t = new Thread() {  
  4.             public void run() {  
  5.                 try {  
  6.                     System.out.println("我被执行了-在sleep()方法前");  
  7.                     // 停止运行10分钟  
  8.                     Thread.sleep(1000 * 60 * 60 * 10);  
  9.                     System.out.println("我被执行了-在sleep()方法后");  
  10.                 } catch (InterruptedException e) {  
  11.                     System.out.println("我被执行了-在catch语句块中");  
  12.                 }  
  13.                 System.out.println("我被执行了-在try{}语句块后");  
  14.             }  
  15.         };  
  16.         // 启动线程  
  17.         t.start();  
  18.         // 在sleep()结束前中断它  
  19.         t.interrupt();  
  20.     }  
  21. }  
     运行结果: 
    1. 我被执行了-在sleep()方法前
    2. 我被执行了-在catch语句块中
    3. 我被执行了-在try{}语句块后

 

    wait()方法也是本地方法,属于Object类,有三个定义:
  1. public final void wait() throws InterruptedException {  
  2.     //do something  
  3. }  
  4. public final native void wait(long timeout) throws InterruptedException;  
  5. public final void wait(long timeout, int nanos) throws InterruptedException {  
  6.     //do something  
  7.  

        wari()和wait(long timeout,int nanos)方法都是基于wait(long timeout)方法实现的。同样地,timeout代表毫秒数,nanos代表纳秒数。当调用了某个对象的wait()方法时,当前运行的线程就会转入等待状态(WAITING),等待别的线程再次调用这个对象的notify()或者notifyAll()方法(这两个方法也是本地方法)唤醒它,或者到了指定的最大等待时间,线程自动醒来。如果线程拥有某个或某些对象的同步锁,那么在调用了wait()后,这个线程就会释放它持有的所有同步资源,而不限于这个被调用了wait()方法的对象。wait()方法同样会被Thread类的interrupt()方法中断,并产生一个InterruptedException异常,效果同sleep()方法被中断一样。 

posted @ 2013-06-25 22:13  tlonge  阅读(228)  评论(0)    收藏  举报