What is difference between wait and sleep methods?
In the java interview, frequently asking is What is difference between wait and sleep methods? Let’s understand main differences between wait and sleep methods in the Thread class in this article.
Differences between wait and sleep
- sleep(): is a static method and It sends current running thread into the “Not Runnable” state. wait(): is a instance method we are calling it using thread object and it is only affected for that object.
- sleep() : Thread weak up after time expire or call interrupt() method. wait() : Thread weak up after time expire or call notify() or notifyAll() method.
- sleep() : Thread doesn’t release any lock or monitor while waiting. wait() : Thread releases the lock or monitor while waiting
Example
public class Main { private static Singleton singletonA = null; private static Singleton singletonB = null; public static void main(String[] args) throws InterruptedException { Thread threadA = new Thread() { @Override public void run() { singletonA = Singleton.getInstance(true); } }; Thread threadB = new Thread() { @Override public void run() { singletonB = Singleton.getInstance(); while (singletonA == null) { System.out.println("SingletonA still null"); } if (singletonA == singletonB) { System.out.println("Both singleton are same"); } else { System.out.println("Both singleton are not same"); } } }; threadA.start(); threadB.start(); } } Singleton : public class Singleton { private static Singleton _instance; public static Singleton getInstance() { if (_instance == null) { synchronized (Singleton.class) { if (_instance == null) _instance = new Singleton(); } } return _instance; } public static Singleton getInstance(boolean isWait) { if (_instance == null) { synchronized (Singleton.class) { if (_instance == null) { if (isWait) { try { // Singleton.class.wait(500);//Using wait Thread.sleep(500);// Using Sleep System.out.println("_instance :"+ String.valueOf(_instance)); } catch (InterruptedException e) { e.printStackTrace(); } } _instance = new Singleton(); } } } return _instance; } }
In Main Class, threadA is using getInstance(boolean isWait) method to create Singeton instance and threadB is using getInstance() method to create Singleton instance.We are starting threadA first and second threadB. threadA will call Thread.sleep(500);
So threadA will moves into Not Runnable State, till 500 milliseconds. Here sleep(500) method is in synchronized method so threadA will not realease any lock and threadB will not get any chance to enter into synchronized block untill threadA release it’s lock.
Run the above program we will get the following output.
Output
_instance :null
Both singleton are same
Singleton instances created by threadA and threadB are same because threadB waiting outside for the lock utill threadA release it’s lock.
Now comment Thread.sleep(500); and enable Singleton.class.wait(500) and run the program, we will get following outpur.
SingletonA still null
SingletonA still null
SingletonA still null
SingletonA still null
SingletonA still null
SingletonA still null
SingletonA still null
SingletonA still null
SingletonA still null
SingletonA still null
SingletonA still null
_instance :Singleton@f0ff4sssdf
SingletonA still null
SingletonA still null
SingletonA still null
Both singleton are not same
Singleton instances created by threadA and threadB are not same because threadB got chance to enter into synchronized block and after 500 milliseconds threadA will start execution from that posting and created one more instance.
I hope folks understood, if you have any queries or suggestion on this article please comment in the below. 🙂
Hello! I am Narayanaswamy founder and admin of narayanatutorial.com. I have been working in the IT industry for more than 12 years. NarayanaTutorial is my web technologies blog. My specialties are Java / J2EE, Spring, Hibernate, Struts, Webservices, PHP, Oracle, MySQL, SQLServer, Web Hosting, Website Development, and IAM(ForgeRock) Specialist
I am a self-learner and passionate about training and writing. I am always trying my best to share my knowledge through my blog.