What is the difference between wait and notify
No Vote. No Up Vote. Sandeep 28 Jun notify In Java : When a thread calls notify method on a particular object, only one thread will be notified which is waiting for the lock or monitor of that object. The thread chosen to notify is random i.
It gets once the calling thread releases the lock of that object. Look here how threads communicate with each other using wait , notify and notifyAll in Java. Any thread calling wait , notify and notifyAll must have lock of that object. In the other words, these methods must be called within synchronized method or synchronized block. Check it with writing a line of code after it. It is the wait calls after notify that releases lock. Hope thats clear. Raju 27 Apr wait commands the thread a to wait for some other thread b to be executed and notify commands the thread to toast an event execution message to the method.
Aishwarya 27 Apr notify and notifyAll methods with wait method are used to for communication between the threads. Sanghamitra 26 Apr WAIT-Object wait methods has three variance, one which waits indefinitely for any other thread to call notify or notifyAll method on the object to wake up the current thread.
IMHO, it was a mistake to add a monitor to java. It's totally irrelevant to most classes, bloats implementations and results in inappropriate use. The ability to declare methods synchronized was also a mistake. It tends to lengthen critical sections and if really needed it isn't could be restricted to some class java. Synchronizable or classes implementing that as an interface rather than bloating the class hierarchy. How are we doing? Please help us improve Stack Overflow.
Take our short survey. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Asked 4 years, 6 months ago. Active 11 months ago. Viewed 7k times. It would be helpful if any real time examples have been provided. Vadim Kotov 7, 8 8 gold badges 45 45 silver badges 60 60 bronze badges.
Possible duplicate of Difference between wait long timeout and join long millis? Add a comment. Post a Comment. Why wait, notify and notifyAll is declared in Object Class instead of Thread is a famous core java interview question which is asked during all levels of Java interview ranging from 2 years, 4years to quite senior level position on java development.
The beauty of this question is that it reflects what does interviewee knows about the wait notify mechanism, how does it sees the whole wait and notify feature, and whether his understanding is not shallow on this topic.
Like Why Multiple inheritances is not supported in Java or why String is final in java there could be multiple answers of why wait and notify is defined in Object class and everyone could justify their reason. In my all interview experience I found that wait and notify still remains most confusing for most Java programmers specially up-to 2 to 3 years and if they asked to write code using wait and notify they often struggle.
So if you are going for any Java interview make sure you have sound knowledge of wait and notify mechanism as well as you are comfortable writing code using wait and notify like Produce Consumer problem or implementing Blocking queue etc. Here are some thoughts on why they should not be in Thread class which make sense to me :. W ait and notify is not just normal meth ods or synchronization utility, more than that they are communication mechanism between two threads in Java.
And Object class is the correct place to make them available for every object if this mechanism is not available via any java keyword like synchronized. Synchronized is to provide mutual exclusion and ensuring thread safety of Java class like race condition while wait and notify are communication mechanism between two thread. Locks are made available on per Object basis , which is another reason wait and notify is declared in Object class rather then Thread class.
In Java in order to enter a critical section of code, Threads needs lock and they wait for lock, they don't know which threads hold lock instead they just know the lock is hold by some thread and they should wait for lock instead of knowing which thread is inside the synchronized block and asking them to release lock.
These are just my thoughts on why the wait and notify method is declared in Object class rather than Thread in Java and you have different versions than me. In reality its another design decision made by Java designers like not supporting Operator overloading in Java. Anyway please post if you have any other convincing reason why wait and notify method should be in Object class and not on Thread.
Update: Lipido has made an insightful comment, which is worth adding here. Some Java articles on Thread you may like.
When to use Thread or Runnable interface in Java? How to Stop Thread in Java. How to implement Thread in Java. Top 15 Thread interview questions in Java. How to prevent deadlock in Java Code. What does the Volatile keyword do in Java.
Share to Twitter Share to Facebook. Labels: core java , core java interview question , Java multithreading Tutorials , thread interview questions. February 5, at PM guru said February 7, at AM Javin run Java program from command prompt said February 7, at AM Javi said February 8, at AM Lipido said One thread will be n February 12, at AM Javin java hashtable example said February 13, at AM Anuj said April 10, at PM Nomad said So, onus is given on the Object class to be able to become shared resource providing it will helper methods like wait ,notify and notifyAll ; June 15, at PM Ahmad said June 25, at AM Niraj Singh said January 22, at AM Unknown said March 2, at AM kailash said September 23, at AM Nitin Banarasi said February 4, at PM Unknown said This concept confuses many Java programmers, both beginners and experienced alike, In fact, this is one of three questions that are very popular on a wait and notifies concept, along with why to wait and notify is defined in Object class and why to wait and notify called from the synchronized method.
In this article, we will focus on the difference between the wait, notify, and notifyAll methods in Java. Here is a couple of main differences between notify and notifyAll method in Java :. First and main difference between notify and notifyAll method is that, if multiple threads are waiting on any locks in Java , notify method to send a notification to only one of the waiting thread while notifyAll informs all threads waiting on that lock.
If you use notify method, It's not guaranteed which thread will be informed, but if you use notifyAll since all thread will be notified, they will compete for lock, and the lucky thread which gets lock will continue. In a way, the notifyAll method is safer because it sends a notification to all threads, so if any thread misses the notification, there are other threads to do the job, while in the case of notify method if the notified thread misses the notification then it could create subtle, hard to debug issues.
Some people argue that using notifyAll can drain more CPU cycles than notify itself but if you really want to sure that your notification doesn't get wasted for any reason, use notifyAll.
0コメント