Martin Desruisseaux wrote:
> Yes, but you don't need a "read" lock for that. A single lock (either 
> the "write" lock or the plain "synchronized" keyword) is enough.
Perhaps we are talking in circles?
>     foo = cache.get(key);
There is a read lock here on the entry; so if a writer is in the process 
of producing the value the read will block. Note this is on the entry - 
not the entire map.
>     if (foo == null) {
>         cache.writeLock(key);  // Lock here
This here represents the commitment from what is now a writer to produce 
a value. This method may block if another writer is in the process of 
producing a value.
>         foo = cache.peek(key);
This represents the writer checking to see if another writer got an 
answer first (it should not happen often but it is a possibility).
>         if (foo == null) {
>             foo = ...
>             cache.put(key, foo);
The put method uses a write lock as well (we are two levels deep in our 
write lock for this one).
>         }
>         cache.releaseLock(key);
our write lock is released; any waiting readers (or writers) are 
released - and they can all see the value we just provided.
>     }
>
> This is the same code than the one you wrote. If 'get' don't block, 
> 'writeLock' block anyway and an other test for 'null' is performed.
Same code; but different locking stratagy ...
>> I wanted to block the readers while the value was being determiend; 
>> the second
>> check (the peek) is for when more than one writer gets started ...
> Yes I agree with this goal, but the result will be identical even if 
> 'get' is not synchronized at all, since the process will block 
> immediately after that anyway (in 'writeLock').
writeLock is a more "serious" locking condition (only one is allowed 
in); by using a read lock we can capture many more threads in the "get" 
method (where many threads are allowed in once reading is available 
again). The effect is on if the map allows concurrent access or not ...

I am a bit confused Martin - why are we talking about this? Did the 
sequence diagrams not work out? Or am I not understanding your suggestion?

Jody

-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Geotools-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-devel

Reply via email to