[Simple-support] What does the Thread-safety guarantee of Persister mean?
Brought to you by:
niallg
|
From: Kiran R. <tec...@gm...> - 2013-01-03 06:19:09
|
The docs for Persister have this to say:
It is fully thread safe and can be shared by multiple threads without
> concerns
>
What does this mean? If multiple threads share an instance of Persister,
and
read operations are interleaved with write operations, will Persister still
"do
the right thing"?
To illustrate, consider the following code:
public class LogicClass{
private final Serializer serializer = new Persister();
public User parse(String aString){
return serializer.read(User.class, aString);
//Exception handling ommitted for brevity.
}
public String frame(Account account){
StringWriter writer = new StringWriter();
serializer.write(account, writer);
//Exception handling ommitted for brevity.
return writer.toString();
}
}
Imagine LogicClass is a singleton (with lower-case 's') shared by multiple
threads. Now it is possible that one thread is parsing User at the same
time that
another is serializing Account. Since LogicClass is a singleton, there is
also
only a single instance of Persister.
How is Persister intended to behave in such circumstances?
--
Regards,
=======
Kiran Rao
http://curioustechizen.blogspot.com/
http://stackoverflow.com/users/570930/curioustechizen
|