java sleep()和wait()的区别 java sleep()和wait()的区别点总结

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

java sleep()和wait()的区别 java sleep()和wait()的区别点总结

小妮浅浅   2021-04-29 我要评论
想了解java sleep()和wait()的区别点总结的相关内容吗,小妮浅浅在本文为您仔细讲解java sleep()和wait()的区别的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:java,sleep(),wait(),下面大家一起来学习吧。

1、区别说明

wait()是Object的方法,sleep()是Thread的方法。

wait()必须采用同步方法,不需要sleep()方法。

线程在同步方法中执行sleep()方法,不释放monitor锁,wait()方法释放monitor锁。

短暂休眠后,sleep()方法会主动退出阻塞,而wait()方法需要在没有指定wait时间的情况下被其他线程中断才能退出阻塞。

2、实例

import java.text.SimpleDateFormat;
import java.util.Date;
public class TestSleepAndWait {
public static void main(String[] args) {
new Thread1().start();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread2().start();
}
}
class Thread1 extends Thread{
private void sout(String s){
System.out.println(s+" "+new SimpleDateFormat("HH:mm:ss:SS").format(new Date()));
}
@Override
public void run() {
sout("enter Thread1.run");
synchronized (TestSleepAndWait.class){//wait只能在同步代码块或者同步方法中使用
sout("Thread1 is going to wait");
try {
TestSleepAndWait.class.wait(); // 这里只能使用持有锁TestSleepAndWait.class.wait(),使用其他对象则报错java.lang.IllegalMonitorStateException
} catch (InterruptedException e) {
e.printStackTrace();
}
sout("after waiting, thread1 is going on");
sout("thread1 is over");
}
}
}
class Thread2 extends Thread{
private void sout(String s){
System.out.println(s+" "+new SimpleDateFormat("HH:mm:ss:SS").format(new Date()));
}
@Override
public void run() {
sout("enter Thread2.run");
synchronized (TestSleepAndWait.class){//wait只能在同步代码块或者同步方法中使用
sout("Thread2 is going to notify");
TestSleepAndWait.class.notify(); 这里只能使用持有锁TestSleepAndWait.class
sout("thread2 is going to sleep 10ms");
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
sout("after sleeping, thread2 is going on");
sout("thread2 is over");
}
}
}

内容扩展:

/**
 * 
 */
package com.b510.test;

/**
 * java中的sleep()和wait()的区别
 * @author Hongten Java学习交流QQ群:589809992 我们一起学Java!
 * @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!!!");
      }
    }
  }
}

 

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们