Reputation: 4864
For example:
static private DateFormat df = new SimpleDateFormat();
public static void format(final Date date) {
for (int i = 0; i < 10; i++)
new Thread(new Runnable() {
public void run() {
System.out.println(df.format(date));
}
});
}
The DateFormat
class is documented as not a synchronized class, but if we use just the format method, it can't change the status of the whole class?
Suppose it's declared private, how can one be sure that the code is thread safe?
What is the best way to fix this code?:
Using a different instance for every thread.
Using a synchronized block.
Upvotes: 8
Views: 4488
Reputation: 5287
As per the docs, it is stated that the format is not thread safe.
Synchronization
Date formats are not synchronized. It is recommended to create separate format instances for each thread. If multiple threads access a format concurrently, it must be synchronized externally.
How to read this ? If you don't have explicit guarantee that some method is thread safe(or its documented as unsafe) then you cannot make any assumptions about it being safe.
However, if you really wish to only use single method which might not be statefull, you can always create high concurrency environment and test for data integrity with and without synchronization.
I have had similar question to this, with Ciphers and RSA. The answer there shows one way how to test specific method of java SE class in general for this. Note however that implementation could change at any point, and by making your own implementation against implementation details rather then interface might cause some unpredictable issues in the future.
Upvotes: 3
Reputation: 70564
I know it is hard to believe, but DateFormat.format() actually modifies the DateFormat's state. For instance, for SimpleDateFormat:
// Called from Format after creating a FieldDelegate
private StringBuffer format(Date date, StringBuffer toAppendTo,
FieldDelegate delegate) {
// Convert input date to time field list
calendar.setTime(date);
where calendar
is a field of DateFormat.
Therefore, I can only recommend you trust the documentation. It may know things you don't.
Upvotes: 2
Reputation: 34628
Therefore, the DateFormat
class is not thread safe. The documentation specifically says:
Date formats are not synchronized. It is recommended to create separate format instances for each thread. If multiple threads access a format concurrently, it must be synchronized externally.
Declaring a field private
does not make your implementation thread-safe. private
merely says that outside classes can't see that field. Let's look at your method:
for (int i=0;i<10;i++)
new Thread(new Runnable(){
public void run(){
System.out.println(df.format(date));
}
});
The Runnable
objects that you create are anonymous classes. Anonymous classes are inner classes, which have access to private fields of their surrounding class. If it wasn't so, your program would not compile - they could not access the df
field.
But they can. So in fact you are having 10 threads that are all accessing your one DateFormat
object, referred to by df
. Since we already know that DateFormat
is not thread-safe, your program is not thread-safe.
df
inside it. You didn't give the class declaration so I don't know what its name is). They have references to the same instance of your class. If both of them call format
at the same time, both will be running DateFormat.format
using the same private df
. Thus, this is not going to be thread-safe.DateFormat
(so you have a new copy every time you call the method). Beware of anonymous classes, though! In your example, even if df
was a local field to the format
method, it would still not be thread-safe because all your threads would be accessing the same copy.Upvotes: 7