views:

43

answers:

2

Is the BlackBerry version of SimpleDateFormat, net.rim.device.api.i18n.SimpleDateFormat thread safe? I know that java.text.SimpleDateFormat is not but there is no mention if net.rim.device.api.i18n.SimpleDateFormat is thread safe or not. Should we assume if it is not stated then it is not thread safe?

+1  A: 

The API doesn't mention.

It suggests using DateFormat.html#getInstance(int) which is some sort of factory method.

So what you can do is to call this method many times from different threads with the same parameter and see if it always returns the same object (compare with ==). If it is, then DateFormat probably caches the returned SimpleDateFormat instances. So since their API allows reuse you may assume that it is thread safe.

cherouvim
interesting, Now in my mind one generic question is coming that how to test an API for thread safety?
Vivart
Maybe that is your next SO question :) Generally you can either inspect the source code (if provided) or write a JUnit test which will hammer the API with millions of calls from 10-20 threads and test for data corruption or exception thrown. Not 100% definite answer though.
cherouvim
+1  A: 

Unless the javadoc for a class explicitly states that it is threadsafe, you should assume that it is not. Even if you can look at the source code and the class appears to be threadsafe, it may be non-threadsafe in the next release ...

The javadoc for DateFormat.getInstance(int) says:

Returns: New SimpleDateFormat instance with the provided style.

So, if you use this method you are guaranteed to get a new instance. Provided that you don't share it between threads, thread safety should not be a concern.

Stephen C