简体   繁体   English

Blackberry的SimpleDateFormat线程安全吗?

[英]Is Blackberry's SimpleDateFormat thread safe?

Is the BlackBerry version of SimpleDateFormat, net.rim.device.api.i18n.SimpleDateFormat thread safe? BlackBerry版的SimpleDateFormat net.rim.device.api.i18n.SimpleDateFormat线程安全吗? 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. 我知道不是java.text.SimpleDateFormat,但是没有提到net.rim.device.api.i18n.SimpleDateFormat是否是线程安全的。 Should we assume if it is not stated then it is not thread safe? 我们是否应该假设如果没有声明,那么它不是线程安全的?

Unless the javadoc for a class explicitly states that it is threadsafe, you should assume that it is not. 除非类的javadoc明确声明它是线程安全的,否则应假定不是。 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: DateFormat.getInstance(int)的Javadoc说:

Returns: New SimpleDateFormat instance with the provided style. 返回:具有提供的样式的新SimpleDateFormat实例。

So, if you use this method you are guaranteed to get a new instance of the class. 因此,如果使用此方法,则可以确保获得该类的实例。 Provided that you don't share it between threads, thread safety should not be a concern. 只要您不在线程之间共享它,就不必担心线程安全性。

The API doesn't mention. API未提及。

It suggests using DateFormat.html#getInstance(int) which is some sort of factory method. 建议使用DateFactory.html#getInstance(int) ,这是某种工厂方法。

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. 如果是,则DateFormat可能缓存返回的SimpleDateFormat实例。 So since their API allows reuse you may assume that it is thread safe. 因此,由于他们的API允许重用,因此您可以假定它是线程安全的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM