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

如何使靜態(tài)日歷線程安全

How to make a static Calendar thread safe(如何使靜態(tài)日歷線程安全)
本文介紹了如何使靜態(tài)日歷線程安全的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

限時(shí)送ChatGPT賬號(hào)..

我想為一些靜態(tài)方法使用日歷并使用靜態(tài)字段:

I'd like to use a Calendar for some static methods and use a static field:

private static Calendar calendar = Calendar.getInstance();

現(xiàn)在我讀到 java.util.Calendar 不是線程安全的.我怎樣才能使這個(gè)線程安全(它應(yīng)該是靜態(tài))?

Now I read java.util.Calendar isn't thread safe. How can I make this thread safe (it should be static)?

推薦答案

如果它不是線程安全的,你就不能做一些線程安全的東西.在 Calendar 的情況下,即使從它讀取數(shù)據(jù)也不是線程安全的,因?yàn)樗梢愿聝?nèi)部數(shù)據(jù)結(jié)構(gòu).

You can't make something thread-safe if it isn't. In the case of Calendar, even reading data from it isn't thread-safe, as it can update internal data structures.

如果可能的話,我建議改用 Joda Time:

If at all possible, I'd suggest using Joda Time instead:

  • 大多數(shù)類型是不可變的
  • 不可變類型是線程安全的
  • 無(wú)論如何,它通常是一個(gè)更好的 API

如果您絕對(duì)必須使用 Calendar,則可以創(chuàng)建一個(gè)鎖定對(duì)象并通過(guò)鎖定來(lái)進(jìn)行所有訪問(wèn).例如:

If you absolutely have to use a Calendar, you could create a locking object and put all the access through a lock. For example:

private static final Calendar calendar = Calendar.getInstance();
private static final Object calendarLock = new Object();

public static int getYear()
{
    synchronized(calendarLock)
    {
        return calendar.get(Calendar.YEAR);
    }
}

// Ditto for other methods

雖然很惡心.您可以只使用 一個(gè) 同步方法,該方法在每次需要時(shí)創(chuàng)建原始日歷的克隆,當(dāng)然...可以通過(guò)調(diào)用 computeFieldscomputeTime 當(dāng)然,您可以使 后續(xù) 讀取操作線程安全,但我個(gè)人不愿意嘗試它.

It's pretty nasty though. You could have just one synchronized method which created a clone of the original calendar each time it was needed, of course... it's possible that by calling computeFields or computeTime you could make subsequent read-operations thread-safe, of course, but personally I'd be loathe to try it.

這篇關(guān)于如何使靜態(tài)日歷線程安全的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

Parsing an ISO 8601 string local date-time as if in UTC(解析 ISO 8601 字符串本地日期時(shí)間,就像在 UTC 中一樣)
How to convert Gregorian string to Gregorian Calendar?(如何將公歷字符串轉(zhuǎn)換為公歷?)
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 日之前日期的日歷到日期轉(zhuǎn)換.公歷到儒略歷切換)
java Calendar setFirstDayOfWeek not working(java日歷setFirstDayOfWeek不起作用)
Java: getting current Day of the Week value(Java:獲取當(dāng)前星期幾的值)
主站蜘蛛池模板: 日本视频在线播放 | 三级av网站 | www.欧美精品 | av播播| 免费高清av| 黄色免费大片 | 亚洲第一色网 | 黄色日本视频 | 国产福利一区二区三区 | 黄色裸体视频 | 中文在线字幕免费观看 | 免费一级黄色片 | 97在线超碰 | 国产激情视频在线观看 | av女优天堂 | 日本激情网站 | 亚洲福利网站 | 一本久| 午夜影院福利 | 成人伊人网 | 日韩中文字幕精品 | 93久久精品日日躁夜夜躁欧美 | 欧美成人a| 亚洲视频在线视频 | 欧美中文字幕在线观看 | 中文字幕日韩高清 | 欧美国产综合 | av片在线看| 四虎在线免费观看视频 | 黄色小说视频网站 | 日韩在线一区二区 | 日韩精品视频在线免费观看 | 可以免费看黄的网站 | 中日韩毛片 | 国产精品一区二区在线播放 | 精品久久网站 | 亚洲综合二区 | 日日av| 国产精品偷乱一区二区三区 | 日韩一级黄色片 | 91亚洲一区|