久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

java.util.Calendar 線程是否安全?

Is java.util.Calendar thread safe or not?(java.util.Calendar 線程是否安全?)
本文介紹了java.util.Calendar 線程是否安全?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我一直在假設 DateCalendar 都不是線程安全的,但是,在最近的一次討論中,一位同事告訴我 日歷是線程安全的.

I've been working under the assumption that neither Date nor Calendar are thread-safe, but, during a recent discussion, a co-worker told me Calendar was thread-safe.

所以,我做了一些研究,但一無所獲.有很多人認為它是線程安全的,也有很多人認為它不是線程安全的.而且,最重要的是,文檔并沒有以任何方式說明任何事情,對于 Calendar,甚至對于 Date 也沒有.

So, I did some research, and came up with nothing. There are plenty people arguing it's thread-safe, and plenty people arguing it's not thread-safe. And, to top it off, the documentation doesn't say anything one way or another, not for Calendar, nor even for Date.

那么,它是什么?

推薦答案

這里是日歷 和 GregorianCalendar

如果您閱讀代碼,您會發現沒有一個實例方法是同步的,并且沒有一個實例字段是 volatile.您還將看到,即使是字段 get 方法也可能導致 Calendar 實例發生變異.而且由于沒有執行同步,不同的線程可能會在這樣的變異操作之后看到日歷對象字段的陳舊版本.

If you read the code you will see that none of the instance methods are synchronized, and none of the instance fields are volatile. You will also see that even the field get methods can cause a Calendar instance to mutate. And since there is no synchronization performed, different threads may see stale versions of a Calendar object's fields following such a mutating operation.

作為記錄,字段 get 方法中的突變操作發生在/調用此方法期間:

For the record, the mutation action in the field get methods happens in / during a call to this method:

 1555 protected void complete()
 1556       {
 1557           if (!isTimeSet)
 1558               updateTime();
 1559           if (!areFieldsSet || !areAllFieldsSet) {
 1560               computeFields(); // fills in unset fields
 1561               areAllFieldsSet = areFieldsSet = true;
 1562           }
 1563       }

簡而言之,Calendar 類不是線程安全的,GregorianCalendar 也不是,因為它繼承了非線程安全的字段和方法.

In short, the Calendar class is not thread-safe, and GregorianCalendar isn't either because it inherits the non-thread-safe fields and methods.

但不要只相信我的話.自己分析源代碼.

But don't just take my word for it. Do your own analysis of the source code.

而且,最重要的是,文檔并沒有以任何方式說明任何事情,對于日歷,甚至對于日期都沒有.

And, to top it off, the documentation doesn't say anything one way or another, not for Calendar, nor even for Date.

如果 javadocs 沒有指定類的線程安全性,那么您應該假設它不是線程安全的.(特別是如果類是可變的設計.)

If the javadocs don't specify the thread-safety of a class, then you should assume that it is not thread-safe. (Especially if the class is mutable by design.)

這篇關于java.util.Calendar 線程是否安全?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Parsing an ISO 8601 string local date-time as if in UTC(解析 ISO 8601 字符串本地日期時間,就像在 UTC 中一樣)
How to convert Gregorian string to Gregorian Calendar?(如何將公歷字符串轉換為公歷?)
Java: What/where are the maximum and minimum values of a GregorianCalendar?(Java:GregorianCalendar 的最大值和最小值是什么/在哪里?)
Calendar to Date conversion for dates before 15 Oct 1582. Gregorian to Julian calendar switch(1582 年 10 月 15 日之前日期的日歷到日期轉換.公歷到儒略歷切換)
java Calendar setFirstDayOfWeek not working(java日歷setFirstDayOfWeek不起作用)
Java: getting current Day of the Week value(Java:獲取當前星期幾的值)
主站蜘蛛池模板: 国产黄色片在线观看 | 久久香蕉精品 | 日韩中文字幕在线视频 | 欧美一区二区在线播放 | 日韩一区二区视频在线观看 | 欧美日韩视频在线 | 三级网站 | 国产精品99久久久久久www | 窝窝午夜精品一区二区 | 九色在线播放 | 成人深夜福利 | 免费av在线网站 | 啊v在线 | 国产精品免费在线 | 天天久久综合 | www.av在线 | 国产寡妇亲子伦一区二区三区四区 | 五月天婷婷激情 | 天天插天天 | 日韩视频一区 | 天天操夜夜操狠狠操 | 欧美美女视频 | 黄色av免费在线观看 | 久草视频免费在线 | www.huangse| 亚洲欧美高清 | 国产精品主播一区二区 | 亚洲第一视频网站 | 欧美日韩成人在线观看 | 午夜999 | 91网站在线免费观看 | 亚洲激情第一页 | 在线免费看av | 国产高清视频在线 | 波多野结衣乳巨码无在线观看 | 国产一级片网站 | 日本久久一区二区 | 成人在线一区二区 | 成人深夜福利视频 | 日韩在线视频免费观看 | 中文字幕手机在线观看 |