File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Doubt on synchronization Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Doubt on synchronization" Watch "Doubt on synchronization" New topic
Author

Doubt on synchronization

Ravindranath Chowdary
Ranch Hand

Joined: Nov 08, 2006
Posts: 71
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

Joined: Jan 29, 2003
Posts: 8791
For #1, see this recent thread.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
ahmed yehia
Ranch Hand

Joined: Apr 22, 2006
Posts: 424

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

Joined: Sep 28, 2004
Posts: 16690
    
  19

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 ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Faraah Dabhoiwala
Greenhorn

Joined: May 30, 2007
Posts: 21
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


<b>Please don't visit my homepage...</b><br /> <a href="http://shadabworld.110mb.com" target="_blank" rel="nofollow">http://shadabworld.110mb.com</a>
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16690
    
  19

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
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Doubt on synchronization
 
Similar Threads
Interview q
Threads
notify(), wait(), notifyall()
Regarding Thread class
Object Class