The difference between sleep() and wait() in Java

learning just encounter these two methods, look up relevant information, and through the program to achieve, the difference:

Each object has a lock to control access to the synchronization, and the Synchronized keyword can interact with an object’s lock to implement Synchronized methods or blocks. sleep() sleep() lock lock! ). wait () method is refers to the current thread to temporarily retreat out the synchronous resource lock, so that other threads are waiting for the resource to get the resources, in turn, to run, only calls the notify () method, before calling wait () thread will lift its state of wait, can go to participate in the competition synchronous resource locks, and enforced. ( note: notify role is equivalent to wake the sleeping man, and would not give his assigned task, that is to say notify calling wait before just let the thread has the right to participate in the thread scheduling ).

2, sleep() can be used anywhere; wait() method can only be used in synchronized methods or synchronized blocks;

3, sleep() is the method of Thread. The call will suspend the specified time of this Thread, but the monitoring will still be maintained, and the object lock will not be released, and it will automatically recover at the time. wait() is an Object method. The call will give up the Object lock and enter the wait queue. The call notify()/notifyAll() will wake up the specified thread or all threads to enter the lock pool and run until the Object lock is acquired again.


to procedures:

public class MultiThread {

	private static class Thread1 implements Runnable{		
		@Override
		public void run() {
			//由于 Thread1和下面Thread2内部run方法要用同一对象作为监视器,如果用this则Thread1和Threa2的this不是同一对象
			//所以用MultiThread.class这个字节码对象,当前虚拟机里引用这个变量时指向的都是同一个对象
			synchronized(MultiThread.class){
				System.out.println("enter thread1 ...");
				System.out.println("thread1 is waiting");
				
				try{
					//释放锁有两种方式:(1)程序自然离开监视器的范围,即离开synchronized关键字管辖的代码范围
					//(2)在synchronized关键字管辖的代码内部调用监视器对象的wait()方法。这里使用wait方法
					MultiThread.class.wait();
				}catch(InterruptedException e){
					e.printStackTrace();
				}
				
				System.out.println("thread1 is going on ...");
				System.out.println("thread1 is being over!");
			}
		}
		
	}
	
	private static class Thread2 implements Runnable{
		@Override
		public void run() {	
			//notify方法并不释放锁,即使thread2调用了下面的sleep方法休息10ms,但thread1仍然不会执行
			//因为thread2没有释放锁,所以Thread1得不到锁而无法执行
			synchronized(MultiThread.class){
				System.out.println("enter thread2 ...");
				System.out.println("thread2 notify other thread can release wait status ...");
				MultiThread.class.notify();
				System.out.println("thread2 is sleeping ten millisecond ...");
				
				try{
					Thread.sleep(10);
				}catch(InterruptedException e){
					e.printStackTrace();
				}
				
				System.out.println("thread2 is going on ...");
				System.out.println("thread2 is being over!");
			}
		}		
	}
	
	public static void main(String[] args) {
		new Thread(new Thread1()).start();
		try{
			Thread.sleep(10);
		}catch(InterruptedException e){
			e.printStackTrace();
		}

		new Thread(new Thread2()).start();
	}

}

program results as shown in the figure below

Read More: