What's the difference between sleep() and wait()?

Keywords: Programming Java

  • sleep() is a static local method of Thread class; wait() is a member local method of Object class
  • sleep() method can be used anywhere; wait() method can only be used in synchronization method or synchronization code block, otherwise Exception in thread "Thread-0" java.lang.IllegalMonitorStateException will be thrown
  • sleep() will sleep the current thread for a specified time, release CPU resources, do not release the object lock, and sleep time will continue to execute until the Automatic wake-up; wait() method gives up the object lock held and enters the waiting queue. When the object is called to notify() / notifyAll() method, it has the opportunity to compete for the object lock and enter the running state
  • JDK1.8 sleep() wait() needs to catch InterruptedException exception

 

Test code

public class TestWaitSleep {
 
	private static Object obj = new Object();
	
	public static void main(String[] args) {
		
		//Test sleep()
		//Test RunnableImpl1 wait(); RunnableImpl2 notify()
		Thread t1 = new Thread(new RunnableImpl1(obj));
		Thread t2 = new Thread(new RunnableImpl2(obj));
		t1.start();
		t2.start();
		
		//Test the RunnableImpl3 wait(long timeout) method
		Thread t3 = new Thread(new RunnableImpl3(obj));
		t3.start();
	}
 
	
}
 
class RunnableImpl1 implements Runnable {
 
	private Object obj;
	
	public RunnableImpl1(Object obj) {
		this.obj = obj;
	}
	
	public void run() {
		System.out.println("run on RunnableImpl1");
		synchronized (obj) {
			System.out.println("obj to wait on RunnableImpl1");
			try {
				obj.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println("obj continue to run on RunnableImpl1");
		}
	}
}
 
class RunnableImpl2 implements Runnable {
 
	private Object obj;
	
	public RunnableImpl2(Object obj) {
		this.obj = obj;
	}
	
	public void run() {
		System.out.println("run on RunnableImpl2");
		System.out.println("Sleep for 3 seconds...");
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		synchronized (obj) {
			System.out.println("notify obj on RunnableImpl2");
			obj.notify();
		}
	}
}
 
class RunnableImpl3 implements Runnable {
 
	private Object obj;
	
	public RunnableImpl3(Object obj) {
		this.obj = obj;
	}
	
	public void run() {
		System.out.println("run on RunnableImpl3");
		synchronized (obj) {
			System.out.println("obj to wait on RunnableImpl3");
			try {
				obj.wait(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println("obj continue to run on RunnableImpl3");
		}
	}
}

Print results

run on RunnableImpl2
//Sleep for 3 seconds
run on RunnableImpl1
obj to wait on RunnableImpl1
run on RunnableImpl3
obj to wait on RunnableImpl3
obj continue to run on RunnableImpl3
notify obj on RunnableImpl2
obj continue to run on RunnableImpl1


 

 

All resources are summarized in the public address.



 

Posted by matanoosh on Fri, 06 Dec 2019 20:48:45 -0800