java 我们可以将 SimpleDateFormat 对象声明为静态对象吗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6137548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can we declare SimpleDateFormat objects as static objects
提问by Joe
SimpleDateFormat monthFormat = new SimpleDateFormat("MMMM");
SimpleDateFormat fullFormat = new SimpleDateFormat("EE MMM dd, HH:mm:ss")
I have several such piece of code which gets invoked often, would it make sense to declare them as static
variables?
我有几个这样的代码经常被调用,将它们声明为static
变量是否有意义?
Is it thread safe to pass dynamic arguments to the format()
method in such cases?
format()
在这种情况下将动态参数传递给方法是否线程安全?
回答by Jigar Joshi
Nothey aren't thread-safe.Use Joda-time's versioninstead.
不,它们不是线程安全的。请改用Joda-time 的版本。
Or make them wrapped in synchronized method and make it thread-safe
或者将它们包裹在同步方法中并使其成为线程安全的
DocSays it clearly
医生说的很清楚
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.
日期格式不同步。建议为每个线程创建单独的格式实例。如果多个线程同时访问一个格式,则必须在外部进行同步。
回答by Magnilex
As of Java 8, this is supported in the new Date API. DateTimeFormatter
is thread-safe and can do the same work as SimpleDateFormat
. Cited from the JavaDoc:
从 Java 8 开始,新的 Date API 支持此功能。DateTimeFormatter
是线程安全的,可以做与SimpleDateFormat
. 引用自 JavaDoc:
A formatter created from a pattern can be used as many times as necessary, it is immutable and is thread-safe.
从模式创建的格式化程序可以根据需要多次使用,它是不可变的并且是线程安全的。
To be extra clear, it is perfectly fine to define a format such as:
更明确地说,定义一个格式是完全没问题的,例如:
private static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy MM dd");
And use it in methods that can be accessed by several threads concurrently:
并在可以被多个线程同时访问的方法中使用它:
String text = date.toString(formatter);
LocalDate date = LocalDate.parse(text, formatter);
回答by dogbane
DateFormat is not thread-safe. If multiple threads use the same DateFormat object without any synchronization you can get unexpected results. So you should either synchronize access to the DateFormat object, use a ThreadLocal variable or use an alternative Date API such as Joda-Time.
DateFormat 不是线程安全的。如果多个线程在没有任何同步的情况下使用相同的 DateFormat 对象,您可能会得到意想不到的结果。因此,您应该同步对 DateFormat 对象的访问,使用 ThreadLocal 变量或使用替代 Date API,例如 Joda-Time。
For more information on how to do this, take a look at this blog post: DateFormat with Multiple Threads
有关如何执行此操作的更多信息,请查看此博客文章:DateFormat with Multiple Threads
回答by ledlogic
An alternative if you are already using Apache Commons:
如果您已经在使用 Apache Commons,则可以使用另一种方法:
回答by Jens Schauder
static shouldn't be a problem.
静态应该不是问题。
Since AFAIK no guarantees are made about thread safety you'd have to check the source code for that. And even if you come to the conclusion that it is thread safe, this might change with the next release. As said in another answer they are not thread safe.
由于 AFAIK 不保证线程安全,因此您必须检查源代码。即使您得出它是线程安全的结论,这可能会随着下一个版本而改变。正如在另一个答案中所说,它们不是线程安全的。
Do you really allocate such a huge amount of threads that giving each thread its own Format is a problem?
你真的分配了如此大量的线程,给每个线程自己的格式是一个问题吗?