久久久久久久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视频精品| 精品成人在线视频 | 国产精品1区| 亚洲a一区| 中文字幕av网 | 国产91亚洲精品一区二区三区 | 国产精品国产a级 | 9191av| 久久青| 欧美va大片| 亚洲色图插插插 | 911精品国产 | 国产在线播 | 久久中文视频 | 久久青草av | 久久久久国产一区二区三区 | 国产做a爱片久久毛片 | 成人小视频在线观看 | 色婷婷国产精品综合在线观看 | 亚洲精品乱码久久久久久蜜桃91 | 久久麻豆精品 | 国产在线小视频 | 久久精品视频在线观看 | 日韩中文一区二区三区 | 中文字幕不卡在线观看 | 日韩欧美国产一区二区三区 | 成人国产精品免费观看 | 九九免费 | 中文字幕亚洲视频 | 久久国产亚洲 | avmans最新导航地址 | 成人在线视频网站 | 久日精品 | 中文字幕一二三 | 精品欧美乱码久久久久久 | 国内精品久久精品 | 国产精品久久久久久久免费大片 | 日韩精品视频一区二区三区 | 国产精品一区二区三区在线 | 成人午夜高清 |