• Post Reply Bookmark Topic Watch Topic
  • New Topic

where to use notify() and synchronized  RSS feed

 
utsav gupta
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Forgive me for my ignorance but as far as I can understand we use notify() method to indicate other threads that the current thread is done with its work here.
However the same purpose can be achieved by using synchronized block as the the other threads will be free to run the sunchronized block once the current thread has exited the block. So why do we need a notify() or a notifyAll() method at all?

Thanks
Utsav.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, If I asked, if you want to give the lock of the object to other thead to do their work in the middle of your work, what will you do? The synchronized is only enough? Could you get the importance of notify() and notifyAll() methods?
 
Ram Narayan.M
Ranch Hand
Posts: 247
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the current thread which has acquired the lock on object, possesses the Object lock for long time execution, the other threads will wait for long time and leads to starvation. This situation is not good... Chances has to be given for other threads to make use of the object lock...

Then the waiting thread has to re-acquire its object lock for that notify() method is called, which notifies a single thread which is waiting for the lock for long time..
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
notify( ) wakes up the first thread that called wait( ) on the same object.
notifyAll( ) wakes up all the threads that called wait( ) on the same object. The highest priority thread will run first but even then both methods doesnot guarantee that they will release the lock when we call them.


But both doesnot even guarantee that they will wake up the threads.
 
Jaikiran Pai
Sheriff
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shanky sohar wrote:notify( ) wakes up the first thread that called wait( ) on the same object.


Not really. See the javadoc of Object.notify() for more details.

 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Normally I use notify and synchronized when I want to communicate between 2 threads.
Suppose you have 2 threads -

thread_1 and thread_2, Lets assume that they want to notify each other about finishing of some process.


Instead of wait and notify, you may want to use lock, condition, await and signal of java.util.concurrent package.
 
sumit anand kumar
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sunny Jain wrote:Normally I use notify and synchronized when I want to communicate between 2 threads.
Suppose you have 2 threads -

thread_1 and thread_2, Lets assume that they want to notify each other about finishing of some process.


Instead of wait and notify, you may want to use lock, condition, await and signal of java.util.concurrent package.

Well in using wait and notify too, you should use the condition checking. because heres something called as abrupt waking in which the thread may wake up even if no body notified it. This is an inconsistent scenario and to avoid illegal access to the resource one should do condition checking and may go to wait state again if the MUTEX flag is false.
The java.util.concurrent package is nice but its a bit complicated for me [].
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sksumit kumar wrote:
Well in using wait and notify too, you should use the condition checking. because heres something called as abrupt waking in which the thread may wake up even if no body notified it. This is an inconsistent scenario and to avoid illegal access to the resource one should do condition checking and may go to wait state again if the MUTEX flag is false.
The java.util.concurrent package is nice but its a bit complicated for me [].


I think you misinterpreted me, I was just giving a small demo. I did not want to hide the main content by showing condition checking and all that.
True, Concurrent package is nice but very less information is available.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!