sleep()和 wait() 区别

本文详细解析了Java中Thread类的sleep方法与Object类的wait方法的区别,包括它们如何影响线程间的同步与执行流程。通过示例代码演示了两者的不同行为,如锁的释放与否,以及它们在同步代码块中的使用方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

public class TestExample 
{	
	public static void main(String[] args) 
	{
		try{
			System.out.println("I'm going to bed");
			/*
			 * Sleep是Thread类的静态方法。
			 * sleep的作用是让线程休眠制定的时间,在时间到达时恢复,也就是说sleep将在接到时间到达事件时恢复线程执行.
			 */
			//Thread.sleep(10);
			/*
			 * Wait是Object的方法,也就是说可以对任意一个对象调用wait方法,
			 * 调用wait方法将会将调用者的线程挂起,直到其他线程调用同一个对象的notify方法才会重新激活调用者.
			 */
			new TestExample().wait();
			System.out.println("I wake up");
	     }catch(Exception e)
	     {
	    	 
	     }
	 }
}

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

public class TestExample 
{	
	public static void main(String[] args) 
	{
		new Thread(new Thread1()).start();
		new Thread(new Thread2()).start();
	 }
	   
    private static class Thread1 implements Runnable
    {
 
       public void run() 
       {
    	   //由于这里的Thread1和下面的Thread2内部run方法要用同一对象作为监视器,我们这里不能用this,
    	   //因为在Thread2里面的this和这个Thread1的this不是同一个对象。我们用TestExample.class这个字节码对象,
    	   //当前虚拟机里引用这个变量时,指向的都是同一个对象。
           synchronized (TestExample.class){
 
              System.out.println("enter thread 1...");
              System.out.println("thread 1 is waiting");
              try{
            	  /*
            	   * 注释第二,三步,放开第一步让Thread1 sleep 10 毫秒,这期间Thread2依旧没有执行。等到10毫秒过后,继续执行Thread1完成后再执行Thread2.
            	   * 结果:
            	   * enter thread 1...
					thread 1 is waiting
					thread 1 is going on...
					thread 1 is being over!
					enter thread 2...
					thread 2 notify other thread can release wait status..
					thread 2 is sleeping ten millisecond...
					thread 2 is going on...
					thread 2 is being over!
            	   */
            	 // Thread.sleep(10);//第一步
            	  /*
            	   * 注释第一步,放开第二步,这个时候Thread2执行,但是因为Thread2没有notify,所以Thread1不会继续执行。
            	   * 结果:
            	   * 	enter thread 1...
						thread 1 is waiting
						enter thread 2...
						thread 2 notify other thread can release wait status..
						thread 2 is sleeping ten millisecond...
						thread 2 is going on...
						thread 2 is being over!

            	   */
            	  TestExample.class.wait();//第二步
     	     }catch(Exception e)
     	     {
     	    	 
     	     }
              System.out.println("thread 1 is going on...");
              System.out.println("thread 1 is being over!");  
           }
       }
      
    }
   
    private static class Thread2 implements Runnable
    {
       public void run() 
       {
           synchronized (TestExample.class)
           {
          
              System.out.println("enter thread 2...");
              System.out.println("thread 2 notify other thread can release wait status..");
              System.out.println("thread 2 is sleeping ten millisecond...");
              /*
        	   * 注释第一步,放开第二,三步,这个时候Thread2执行,而且因为Thread2发出notify,所以Thread1继续执行。
        	   * 结果:
        	   * 	enter thread 1...
					thread 1 is waiting
					enter thread 2...
					thread 2 notify other thread can release wait status..
					thread 2 is sleeping ten millisecond...
					thread 2 is going on...
					thread 2 is being over!
					thread 1 is going on...
					thread 1 is being over!

        	   */
              TestExample.class.notifyAll(); //第三步
              System.out.println("thread 2 is going on...");
              System.out.println("thread 2 is being over!");
             
           }
       }
      
    }
}

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

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

 

内容概要:本文档详细介绍了Analog Devices公司生产的AD8436真均方根-直流(RMS-to-DC)转换器的技术细节及其应用场景。AD8436由三个独立模块构成:轨到轨FET输入放大器、高动态范围均方根计算内核精密轨到轨输出放大器。该器件不仅体积小巧、功耗低,而且具有广泛的输入电压范围快速响应特性。文档涵盖了AD8436的工作原理、配置选项、外部组件选择(如电容)、增益调节、单电源供电、电流互感器配置、接地故障检测、三相电源监测等方面的内容。此外,还特别强调了PCB设计注意事项误差源分析,旨在帮助工程师更好地理解应用这款高性能的RMS-DC转换器。 适合人群:从事模拟电路设计的专业工程师技术人员,尤其是那些需要精确测量交流电信号均方根值的应用开发者。 使用场景及目标:①用于工业自动化、医疗设备、电力监控等领域,实现对交流电压或电流的精准测量;②适用于手持式数字万用表及其他便携式仪器仪表,提供高效的单电源解决方案;③在电流互感器配置中,用于检测微小的电流变化,保障电气安全;④应用于三相电力系统监控,优化建立时间转换精度。 其他说明:为了确保最佳性能,文档推荐使用高质量的电容器件,并给出了详细的PCB布局指导。同时提醒用户关注电介质吸收泄漏电流等因素对测量准确性的影响。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值