Hello,
i have the following problem: in my application, one or more thread must
be able to read some information, all together if they want to. At the
same time, one thread must be able to modify the informations, and the
readers must not gain access when the writer thread is writing.
But one reader must not prevent another reader to access the informations.
Let's say the informations is in the class:
public class Informations {
/** Write access. */
public void modify() {
// some stuff...
}
/** Read access. */
public Object getValue(String key) {
// some code...
}
}
What synchronized/flag/else should i put in order to make it work?
TIA
Thomas Hawtin - 04 May 2006 15:03 GMT
> i have the following problem: in my application, one or more thread must
> be able to read some information, all together if they want to. At the
> same time, one thread must be able to modify the informations, and the
> readers must not gain access when the writer thread is writing.
> What synchronized/flag/else should i put in order to make it work?
http://download.java.net/jdk6/docs/api/java/util/concurrent/locks/ReadWriteLock.html
(from 1.5, or use the 1.4 backport)
Tom Hawtin

Signature
Unemployed English Java programmer
http://jroller.com/page/tackline/
mtp - 04 May 2006 16:22 GMT
Thomas Hawtin a écrit :
>> i have the following problem: in my application, one or more thread
>> must be able to read some information, all together if they want to.
[quoted text clipped - 8 lines]
>
> Tom Hawtin
Thx to both of you.
Where can i get the "backport" (since i work on 1.4 ?).
Thomas Hawtin - 04 May 2006 15:37 GMT
> Thomas Hawtin a écrit :
>>
>> http://download.java.net/jdk6/docs/api/java/util/concurrent/locks/ReadWriteLock.html
>>
>> (from 1.5, or use the 1.4 backport)
> Thx to both of you.
>
> Where can i get the "backport" (since i work on 1.4 ?).
http://dcl.mathcs.emory.edu/util/backport-util-concurrent/
Tom Hawtin

Signature
Unemployed English Java programmer
http://jroller.com/page/tackline/
Gordon Beaton - 04 May 2006 15:55 GMT
> i have the following problem: in my application, one or more thread
> must be able to read some information, all together if they want to.
> At the same time, one thread must be able to modify the
> informations, and the readers must not gain access when the writer
> thread is writing.
[..]
> What synchronized/flag/else should i put in order to make it work?
It's not so simple to implement this correctly using synchronized, but
it's already been done for you.
See java.util.concurrent.locks.ReadWriteLock.
/gordon

Signature
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e