久久久久久久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)前星期幾的值)
主站蜘蛛池模板: 五月婷婷亚洲 | 国产精品视频在线播放 | h视频免费在线观看 | 日本又色又爽又黄又高潮 | 欧美亚洲视频在线观看 | 国产99视频精品免视看9 | 视频在线一区二区 | 99热热热| 国产在线网址 | 99久久婷婷国产精品综合 | 日韩在线电影 | 亚洲视频一区二区三区四区 | 国产精品美女久久久久aⅴ国产馆 | 九九在线视频 | 黑人巨大精品欧美一区二区免费 | 亚洲综合无码一区二区 | 日韩在线欧美 | 久久久久亚洲精品中文字幕 | 国产精品一区二区三区在线 | 国产高清精品在线 | xxxxx免费视频| 伊人网影院 | 亚洲一区二区在线播放 | 日韩欧美中文 | 国产aⅴ | 久久免费精品视频 | 三级欧美| 福利片在线| 99久久精品一区二区毛片吞精 | 91爱爱·com| 日韩精品免费播放 | 97精品国产一区二区三区 | 密乳av| 色综合美女 | 九九久久99| 男人天堂久久 | 亚洲性网| 亚洲人成网站777色婷婷 | 亚洲国产一区二区三区, | 亚洲三区在线播放 | 国产精久久久久久久 |