久久久久久久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:獲取當前星期幾的值)
主站蜘蛛池模板: 午夜欧美一区二区三区在线播放 | 欧美群妇大交群中文字幕 | 放个毛片看看 | 欧美另类视频 | 日本精品一区二区三区在线观看 | 一区在线播放 | 在线视频亚洲 | 久久国产电影 | 成人免费视频一区二区 | 精品久久香蕉国产线看观看亚洲 | 久久亚洲视频网 | 亚洲国产一区在线 | 乱码av午夜噜噜噜噜动漫 | 欧美三级久久久 | 精品久久久久久亚洲精品 | 7777精品伊人久久精品影视 | 啪啪免费| 国产综合久久久久久鬼色 | 日韩在线不卡视频 | 精品欧美一区二区三区久久久 | h免费观看 | 久久精品中文字幕 | 日韩在线看片 | 午夜精品久久 | 91精品国产777在线观看 | 91精品国产91久久综合桃花 | 亚洲一区二区三区免费在线 | 国产精品视频久久久 | 夜夜操天天干 | 欧美美女一区二区 | 国产91丝袜在线播放 | 日日操夜夜操天天操 | 亚洲精品一区二区三区在线观看 | 激情欧美日韩一区二区 | 成人在线免费观看 | 久久在线| 免费一区二区三区 | 欧美精品欧美精品系列 | 情侣黄网站免费看 | 在线视频一区二区 | 久久久人成影片免费观看 |