Re: [Simple-support] What does the Thread-safety guarantee of Persister mean?
Brought to you by:
niallg
|
From: Niall G. - Y. <Nia...@yi...> - 2013-01-03 06:46:16
|
Yes, it will work in this scenario
From: Kiran Rao [mailto:tec...@gm...]
Sent: Thursday, 3 January 2013 5:19 PM
To: simple-support
Subject: [Simple-support] What does the Thread-safety guarantee of Persister mean?
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
|