简体   繁体   English

Java的TimeZone是线程安全的吗?

[英]Is Java's TimeZone thread-safe?

I wanted my application to just have one TimeZone object which will be used by many SimpleDateFormat and Calendar objects from the other places concurrently. 我希望我的应用程序只有一个TimeZone对象,它将被其他地方的许多SimpleDateFormatCalendar对象同时使用。 This is to avoid having to always do TimeZone.getTimeZone(ID) . 这是为了避免总是做TimeZone.getTimeZone(ID)

I know SimpleDateFormat and Calendar classes are not thread safe, which is why I configure one thread to always create new instances of them. 我知道SimpleDateFormatCalendar类不是线程安全的,这就是为什么我配置一个线程来始终创建它们的新实例。 But what about TimeZone ? TimeZone怎么样? It is not clear to me whether I can do the following safely: 我不清楚我是否可以安全地执行以下操作:

final TimeZone tz = TimeZone.getTimeZone("GMT");
...
//Thread 1.
Thread t1 = new Thread(Runnable(){
    public void run()
    {
        Calendar cal = Calendar.getInstance(tz);
        ...
        SimpleDateFormat sdf = new SimpleDateFormat();
        sdf.setTimeZone(tz);
        ...
    }
});
t1.start();
...
//Thread 2.
Thread t2 = new Thread(Runnable(){
    public void run()
    {
        Calendar cal = Calendar.getInstance(tz);
        ...
        SimpleDateFormat sdf = new SimpleDateFormat();
        sdf.setTimeZone(tz);
        ...
    }
});
t2.start();
...

Thanks! 谢谢!

I looked once at the source code for it and came to a conclusion that it was not. 我看了一下它的源代码并得出结论它不是。

Look at JodaTime timezone class. 看看JodaTime时区课程。 It says in javadoc that it's thread-safe and immutable. 它在javadoc中说它是线程安全且不可变的。

It is not. 它不是。

And you don't need it. 你不需要它。 You code uses it as: 您的代码将其用作:

final TimeZone tz = TimeZone.getTimeZone("GMT");
 ......
// thread 1 SimpleDateFormat instance
sdf.setTimeZone(tz);

// thread 2 SimpleDateFormat instance
sdf.setTimeZone(tz);

You have two threads using the same time zone, but since you are not modifying it you don't need it to be thread safe. 你有两个线程使用相同的时区,但由于你没有修改它,你不需要它是线程安全的。

The only thing that you can modify is the ID, but even then you're ok, for the rest of the attributes are read-only 您可以修改的唯一内容是ID,但即便如此,您也可以,因为其余属性是只读的

The only way you can get into trouble is by changing the id and caching your self the timezone, if you get it always from the TimeZone.getTimeZone() you are safe too, because that method is thread safe. 你可以遇到麻烦的唯一方法是更改​​id并自行缓存你的时区,如果你总是从TimeZone.getTimeZone()获得它也是安全的,因为该方法线程安全的。

There is no explicit documentation that states TimeZone is thread-safe, so the safest route is assume that it isn't thread safe. 没有明确的文档说明TimeZone是线程安全的,所以最安全的路径是假设它不是线程安全的。

The TimeZone class has two instance mutators, relating to ID and its GMT offset. TimeZone类有两个实例变更器,与ID及其GMT偏移有关。 Common sense would say that Calendar.getInstance and SimpleDateFormat have no business modifying TimeZone object state (in the first case, it's being used as a look-up key, and in the second, as formatting context). 常识会说Calendar.getInstance和SimpleDateFormat没有业务修改TimeZone对象状态(在第一种情况下,它被用作查找键,在第二种情况下,用作格式化上下文)。

Common sense or certainty - your choice. 常识或确定性 - 您的选择。

您没有修改TimeZone,因此无论TimeZone的底层实现是否是线程安全的(除了静态方法,如(getTimeZone / getDefault / setDefault)),您的代码应该是线程安全的。

This appears to be OK. 似乎没问题 If you were relying on TimeZone.getDefault() that would be a different story. 如果你依赖于TimeZone.getDefault()那将是一个不同的故事。 As another thread could potentially be calling TimeZone.setDefault(). 由于另一个线程可能正在调用TimeZone.setDefault()。

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

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