sleep()和 wait() 区别

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不需要捕获异常

 

你可能感兴趣的:(wait,sleep)