黑马程序员技术交流社区

标题: java中的sleep()和wait()的区别 [打印本页]

作者: linder_qzy    时间: 2015-3-11 16:53
标题: java中的sleep()和wait()的区别

对于sleep()方法,首先要知道该方法是属于Thread类中的。而wait()方法,则是属于Object类中的。

sleep()方法导致了程序暂停执行指定的时间,让出cpu该其他线程,但是他的监控状态依然保持者,当指定的时间到了又会自动恢复运行状态。

在调用sleep()方法的过程中,线程不会释放对象锁。

而当调用wait()方法的时候,线程会放弃对象锁,进入等待此对象的等待锁定池,只有针对此对象调用notify()方法后本线程才进入对象锁定池准备

获取对象锁进入运行状态。

什么意思呢?举个来列子说明:

  1. package heima.day04;
  2. public class TestD {

  3.         public static void main(String[] args) {
  4.                 new Thread(new Thread1()).start();
  5.                 try {
  6.                         Thread.sleep(5000);
  7.                 } catch (Exception e) {
  8.                         e.printStackTrace();
  9.                 }
  10.                 new Thread(new Thread2()).start();
  11.         }
  12.         private static class Thread1 implements Runnable {
  13.                 public void run() {
  14.                         synchronized (TestD.class) {
  15.                                 System.out.println("enter thread1...");
  16.                                 System.out.println("thread1 is waiting...");
  17.                                 try {
  18.                                         //调用wait()方法,线程会放弃对象锁,进入等待此对象的等待锁定池
  19.                                         TestD.class.wait();
  20.                                 } catch (Exception e) {
  21.                                         e.printStackTrace();
  22.                                 }
  23.                                 System.out.println("thread1 is going on ....");
  24.                                 System.out.println("thread1 is over!!!");
  25.                         }
  26.                 }
  27.         }
  28.         private static class Thread2 implements Runnable {

  29.                 public void run() {
  30.                         synchronized (TestD.class) {
  31.                                 System.out.println("enter thread2....");
  32.                                 System.out.println("thread2 is sleep....");
  33.                                 //只有针对此对象调用notify()方法后本线程才进入对象锁定池准备获取对象锁进入运行状态。
  34.                                 TestD.class.notify();
  35.                                 //==================
  36.                                 //区别
  37.                                 //如果我们把代码:TestD.class.notify();给注释掉,即TestD.class调用了wait()方法,但是没有调用notify()
  38.                                 //方法,则线程永远处于挂起状态。
  39.                                 try {
  40.                                         //sleep()方法导致了程序暂停执行指定的时间,让出cpu该其他线程,
  41.                                         //但是他的监控状态依然保持者,当指定的时间到了又会自动恢复运行状态。
  42.                                         //在调用sleep()方法的过程中,线程不会释放对象锁。
  43.                                         Thread.sleep(5000);
  44.                                 } catch (Exception e) {
  45.                                         e.printStackTrace();
  46.                                 }
  47.                                 System.out.println("thread2 is going on....");
  48.                                 System.out.println("thread2 is over!!!");
  49.                         }
  50.                 }
  51.         }
  52. }
  53. /*
  54. * 运行结果
  55. * enter thread1...
  56.    thread1 is waiting...
  57.    enter thread2....
  58.    thread2 is sleep....
  59.    thread2 is going on....
  60.    thread2 is over!!!
  61.    thread1 is going on ....
  62.    thread1 is over!!!
  63. */
复制代码

如果注释掉代码

  1. Thread.sleep(5000);
复制代码
运行效果:

  1. enter thread1...
  2. thread1 is waiting...
  3. enter thread2....
  4. thread2 is sleep....
  5. thread2 is going on....
  6. thread2 is over!!!
  7. thread1 is going on ....
  8. thread1 is over!!!
复制代码









欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2