Java中的sleep()和wait()的区别

来源:互联网 发布:超级mac炮 编辑:程序博客网 时间:2024/05/23 02:05

1.sleep()方法是属于Thread类中的;wait()方法是属于Object类中的

2.sleep()方法让程序暂停执行一段时间,让出cpu给其他线程,但他的监控状态依然保持着,当指定时间到了会自动恢复运行状态在调用sleep()方法中,线程不会释放对象锁;

3.调用wait()方法时,线程会放弃对象锁,进入等待此对象的等待锁定池,只有针对此对象调用notify()方法后本线程才进入对象锁定池准备,获得对象锁进入运行状态

4.sleep()方法必须捕获异常,因为线程在sleep期间,有可能被对象调用它的interrupt()方法中断

中断一个线程会导致一个InterruptedException异常产生,如果程序不捕获异常,线程会异常终止,进入TERMINATED状态,如果程序捕获了异常,那么就会执行catch语句块

5.wait,notify和notifyAll只能在同步控制方法或者同步控制块里面使用,而sleep可以在任何地方使用


/** *  */package com.b510.test;/** * java中的sleep()和wait()的区别 * @author Hongten * @date 2013-12-10 */public class TestD {    public static void main(String[] args) {        new Thread(new Thread1()).start();        try {            Thread.sleep(5000);        } catch (Exception e) {            e.printStackTrace();        }        new Thread(new Thread2()).start();    }        private static class Thread1 implements Runnable{        @Override        public void run(){            synchronized (TestD.class) {            System.out.println("enter thread1...");                System.out.println("thread1 is waiting...");            try {                //调用wait()方法,线程会放弃对象锁,进入等待此对象的等待锁定池                TestD.class.wait();            } catch (Exception e) {                e.printStackTrace();            }            System.out.println("thread1 is going on ....");            System.out.println("thread1 is over!!!");            }        }    }        private static class Thread2 implements Runnable{        @Override        public void run(){            synchronized (TestD.class) {                System.out.println("enter thread2....");                System.out.println("thread2 is sleep....");                //只有针对此对象调用notify()方法后本线程才进入对象锁定池准备获取对象锁进入运行状态。                TestD.class.notify();                //==================                //区别                //如果我们把代码:TestD.class.notify();给注释掉,即TestD.class调用了wait()方法,但是没有调用notify()                //方法,则线程永远处于挂起状态。                try {                    //sleep()方法导致了程序暂停执行指定的时间,让出cpu该其他线程,                    //但是他的监控状态依然保持者,当指定的时间到了又会自动恢复运行状态。                    //在调用sleep()方法的过程中,线程不会释放对象锁。                    Thread.sleep(5000);                } catch (Exception e) {                    e.printStackTrace();                }                System.out.println("thread2 is going on....");                System.out.println("thread2 is over!!!");            }        }    }}


运行效果enter thread1...thread1 is waiting...enter thread2....thread2 is sleep....thread2 is going on....thread2 is over!!!thread1 is going on ....thread1 is over!!!

如果注释掉代码TestD.class.notify();

运行效果enter thread1...thread1 is waiting...enter thread2....thread2 is sleep....thread2 is going on....thread2 is over!!!


且程序一直处于挂起状态


sleep()方法和wait()方法都成产生让当前运行的线程停止运行的效果,这是它们的共同点。


public static native void sleep(long millis) throws InterruptedException;  public static void sleep(long millis, int nanos) throws InterruptedException {      //other code  }  

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


为了更好地理解interrupt()效果,我们来看一下下面这个例子: 
public class InterruptTest {      public static void main(String[] args) {          Thread t = new Thread() {              public void run() {                  try {                      System.out.println("我被执行了-在sleep()方法前");                      // 停止运行10分钟                      Thread.sleep(1000 * 60 * 60 * 10);                      System.out.println("我被执行了-在sleep()方法后");                  } catch (InterruptedException e) {                      System.out.println("我被执行了-在catch语句块中");                  }                  System.out.println("我被执行了-在try{}语句块后");              }          };          // 启动线程          t.start();          // 在sleep()结束前中断它          t.interrupt();      }  }  

运行结果: 我被执行了-在sleep()方法前我被执行了-在catch语句块中我被执行了-在try{}语句块后










0 0
原创粉丝点击