So basically wait, notify and notify all is a matter of convienience from the developer point of view to improve performance by making other threads not checking the status of locks till they have been notified by the lock holding thread.
Am I right?
No.
When you invoke wait() on an Object you are halting the current thread of execution until another thread invokes notify() or notifyAll() on the Object you invoked wait() on.
What this does is allow a developer to halt a thread until some condition is met. In the Producer/Consumer example the Consumer would check for the availability of what it wanted, if it was not available it would invoke wait() and consequently halt it's execution. Once that thread starts up again, either because another thread invoked notify() or notifyAll() on the same Object it invoked wait() on, or because it was interrupted, it then checks for availability again. If it's still not available, it waits once again. If it is available, it gets what it wanted and continues on it's merry way returning from that method.
The Producer does the same thing, but rather than waiting for something to be available to get it waits until there's nothing available to get and creates one so that one will be available.
In my case this locking contention by other threads can happen only once during the whole day like (5.00 in the morning) as standing data will be refreshed once.
Critical Sections:
isRefreshRequired(Consumer)
RefreshCache(Producer).
I did not bother about wait, notify and notify all. Now Ideally I just have to sync both the above methods, as this is code is present as a part of every request i just have sync on producer method and let consumer call producer more than once based on the number of thread concurrency at the time but i dont want all threads to run producer at the same time.
Should be fine I gues your opinion?
Regards
Farouk
I'm sorry, I don't understand exactly what you're doing. As I understand it you don't want more than one thread to try and refresh your data at the same time. If the code checking if a refresh is required and the code doing the actual refreshing is synchronized
you should be fine. However, what's to prevent a consumer from getting corrupted data in the
middle of a refresh? They all need to be synchronized. If your implementation is such that a thread is unable to get data while the data is being refreshed then any thread that tries to is effectively already waiting until it's done anyway.
That would be a case where it would work fine. Though it should be noted that if data is mutable it could be changed by other threads concurrently. But that's beyond the scope of the question I think.
[ November 29, 2005: Message edited by: Ken Blair ]