简体   繁体   English

为什么Java的SimpleDateFormat类非线程安全?

[英]Why is Java's SimpleDateFormat class non thread safe?

The question regards Java's SimpleDateFormat class. 问题涉及Java的SimpleDateFormat类。 I've read in the documentation that 我在文档中读过

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. 如果多个线程同时访问格式,则必须在外部进行同步。

Obviously, I've read that after being hit in the most obscure way by it. 很显然,我读过,被击中由它最模糊的方式之后

Does anybody know why would it be necessary to have state information in class members accessed by the "format" method for example? 有人知道为什么有必要在“格式”方法访问的类成员中拥有状态信息吗?

Is it a speed optimization of some sort? 这是某种速度优化吗? I couldn't come up with a valid reason. 我无法提出正确的理由。

Your answer pretty much lies here: 你的答案几乎就在这里:

/* * (C) Copyright Taligent, Inc. 1996 - All Rights Reserved / * *(C)版权所有Taligent,Inc。1996 - 保留所有权利

Prior to 1.4 hotspot writing multi-threaded code in Java was primarily a homework assignment for graduate students. 在1.4热点编写之前,Java中的多线程代码主要是研究生的家庭作业。 The language had been around 10 years before the serious arrival of web-application servers and rise of the container-driven highly concurrent systems that most of us (non-android anyway) spend the vast majority of our time working in. 在Web应用程序服务器的严重到来以及容器驱动的高度并发系统的兴起之前,这种语言已经存在了大约10年,我们大多数人(非机器人无论如何)都花费了我们绝大部分时间。

In 1996, Garbage Collection was a very slow and painful process that generally made your UI pause and look locked up while it happened, like wise creating new objects was considered very expensive (creating contiguous memory space when you're fighting with Windows 95 for a share of 4MB of physical memory with no L2 CPU cache takes some time.....). 在1996年,垃圾收集是一个非常缓慢和痛苦的过程,通常会使你的UI暂停并在发生时看起来被锁定,就像明智地创建新对象被认为是非常昂贵的(当你与Windows 95争夺时,创建连续的内存空间没有L2 CPU缓存的4MB物理内存共享需要一些时间.....)。

So in an environment where multi-threading is extremely rare, and memory is at a premium (your average user is probably still on a 486 or Pentium 1 with 8MB or even 4MB of system memory....) it makes perfect sense to re-use a single instance of Calendar as much as possible, Calendar itself being something of a clunky beast. 因此,在多线程非常罕见且内存非常宝贵的环境中(普通用户可能仍然使用486或Pentium 1,8MB甚至4MB的系统内存......)。 - 尽可能使用日历的单个实例,日历本身就是一个笨重的野兽。

We can scoff today at what a horrible practice it is for a class like that to be stateful, but it can also be easily defended as the right choice at the time. 我们今天可以嘲笑这样一个类似于有状态的可怕实践,但它也可以很容易地被当作正确的选择。

Defending Sun's obsession with 100% backward compatibility and never updating it is another matter of course! 捍卫Sun对100%向后兼容性的痴迷,永不更新它是另一回事!

It looks like it is done for the performance reason. 它似乎是出于性能原因而完成的。 I see no other explanations for it. 我没有看到其他任何解释。

There is a good summary on how it works here: Why is Java's SimpleDateFormat not thread-safe? 关于它如何工作有一个很好的总结: 为什么Java的SimpleDateFormat不是线程安全的?

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

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