Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Doubt on synchronization  RSS feed

 
Ravindranath Chowdary
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,
Can anyone please give clear idea on this.

1. Why the wait/notify/notifyAll are written in Object class?

2. Why the wait/notify/notifyAll need to be called from synchronized blocks or statements?

Thanks,
Ravindra.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For #1, see this recent thread.
 
ahmed yehia
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Why the wait/notify/notifyAll need to be called from synchronized blocks or statements?

To call these methods, a thread must be the owner of the lock for the object, this is done by synchronizing the code that makes these method calls. This will prevent race- condition which might happen if 2 or more threads make these calls concurrently. Maybe there is more on that..
 
Henry Wong
author
Sheriff
Posts: 22843
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
2. Why the wait/notify/notifyAll need to be called from synchronized blocks or statements?


Short Answer. Every threading system -- Solaris, POSIX, Windows, etc. -- require it. It is not possible to wait on a condition variable without holding the mutex assigned to the variable. So Java does it because everyone else does it...


Long Answer. In general, you don't send a notification, or wait on a condition blindly. You have to check a flag or state. And since this flag is accessed by many threads, it must be done under synchronization.

However, if it is deemed that you must wait, because the flag isn't correct, then you must released the lock and wait. Unfortunately, there is a race condition, as it is possible to lose a notification between the time it takes to release the lock and to perform the wait.

So... The wait() method will release the lock for you -- and do so in such a way as to not have this race condition. And since the wait() method is going to release the lock for you, it is required that you actually hold the lock.

BTW, there is no reason the notify() requires this. It is mostly done to be consistent.

Henry
[ October 12, 2007: Message edited by: Henry Wong ]
 
Faraah Dabhoiwala
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW, there is no reason the notify() requires this


But we are having a notify() function and it must be doing something


can you please explain wait method in someother way i found it more like OS stuff
 
Henry Wong
author
Sheriff
Posts: 22843
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shadab Wadiwala wrote:
But we are having a notify() function and it must be doing something


Aside from checking to ensure that the lock, for the object being used to notify is held by the current thread, it doesn't do anything else with the lock.

Shadab Wadiwala wrote:
can you please explain wait method in someother way i found it more like OS stuff


The wait() method also ensure that the lock is held by the current thread, places the thread in a wait state, and frees the lock. It also...

1. Frees all the grabs on the lock.... meaning if the thread acquired the lock 5 times, it will free all five acquisitions on the lock.
2. Frees lock and places thread in wait state, in such a fashion that no notification is lost. It is not possible to lose a notification if it comes in during this critical time.
3. Upon return, it will reacquire the locks... meaning if the thread had acquired the lock 5 times, it will get all 5 of those lock grabs back on return.

Henry



Note: The topic that caused this topic to be awoken....

http://www.coderanch.com/t/233995/Threads-Synchronization/java/synchronization
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!