[EMAIL PROTECTED] a écrit :
>> 1) Either the "foo" value was already cached (in which case
>> the 'get' method perform a very short locking since it
>> executes nothing more than "return value" - we could even
>> use no lock at all for such a trivial task, since returning
>> a single reference is already an atomic operation on JVM.
>
> Except for one very important point - when a writer is updating the entry that
> get method is going to block until a value is ready.
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.
foo = cache.get(key); // No lock at all.
if (foo == null) {
cache.writeLock(key); // Lock here
foo = cache.peek(key);
if (foo == null) {
foo = ...
cache.put(key, foo);
}
cache.releaseLock(key);
}
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.
>> 2) Either the "foo" value is already in process of being
>> computed, in which case "get" will block anyway (or return
>> "null" is not synchronized) and "peek" will block too.
>
> Peek will not block; we can enter the read write lock more than once (so
> methods
> can be recursive if needed).
I should said 'writeLock(key)' will block. The result is the same.
> 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').
Martin
-------------------------------------------------------------------------
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