如何使静态日历线程安全

How to make a static Calendar thread safe(如何使静态日历线程安全)
本文介绍了如何使静态日历线程安全的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我想为一些静态方法使用日历并使用静态字段:

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

private static Calendar calendar = Calendar.getInstance();

现在我读到 java.util.Calendar 不是线程安全的.我怎样才能使这个线程安全(它应该是静态)?

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

推荐答案

如果它不是线程安全的,你就不能做一些线程安全的东西.在 Calendar 的情况下,即使从它读取数据也不是线程安全的,因为它可以更新内部数据结构.

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:

  • 大多数类型是不可变的
  • 不可变类型是线程安全的
  • 无论如何,它通常是一个更好的 API

如果您绝对必须使用 Calendar,则可以创建一个锁定对象并通过锁定来进行所有访问.例如:

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

虽然很恶心.您可以只使用 一个 同步方法,该方法在每次需要时创建原始日历的克隆,当然...可以通过调用 computeFieldscomputeTime 当然,您可以使 后续 读取操作线程安全,但我个人不愿意尝试它.

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.

这篇关于如何使静态日历线程安全的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Month is not printed from a date - Java DateFormat(月份不是从日期打印的 - Java DateFormat)
java get week of year for given a date(java获取给定日期的一年中的一周)
Get the number of days, weeks, and months, since Epoch in Java(获取自 Java 中的 Epoch 以来的天数、周数和月数)
Is there a good way to get the date of the coming Wednesday?(有什么好方法可以得到即将到来的星期三的日期吗?)
Calendar view for Android GingerBread and before (APIlt;11)(Android GingerBread 及之前的日历视图 (API11))
How to save and retrieve Date in SharedPreferences(如何在 SharedPreferences 中保存和检索日期)