java - "Java DateFormat is not threadsafe"这会导致什么?

标签 java multithreading date-format

每个人都注意 Java DateFormat 不是线程安全的,我从理论上理解这个概念。

但我无法想象我们会因此面临哪些实际问题。比如说,我在一个类中有一个 DateFormat 字段,并且在多线程环境中的类(格式化日期)的不同方法中使用了相同的字段。

这会不会导致:

  • 任何异常,例如格式异常
  • 数据不一致
  • 还有其他问题吗?

另外,请解释原因。

最佳答案

让我们试试吧。

这是一个程序,其中多个线程使用共享的SimpleDateFormat

计划:

public static void main(String[] args) throws Exception {

    final DateFormat format = new SimpleDateFormat("yyyyMMdd");

    Callable<Date> task = new Callable<Date>(){
        public Date call() throws Exception {
            return format.parse("20101022");
        }
    };

    //pool with 5 threads
    ExecutorService exec = Executors.newFixedThreadPool(5);
    List<Future<Date>> results = new ArrayList<Future<Date>>();

    //perform 10 date conversions
    for(int i = 0 ; i < 10 ; i++){
        results.add(exec.submit(task));
    }
    exec.shutdown();

    //look at the results
    for(Future<Date> result : results){
        System.out.println(result.get());
    }
}

运行几次,你会看到:

异常(exception)情况:

这里有几个例子:

1.

Caused by: java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
    at java.lang.Long.parseLong(Long.java:431)
    at java.lang.Long.parseLong(Long.java:468)
    at java.text.DigitList.getLong(DigitList.java:177)
    at java.text.DecimalFormat.parse(DecimalFormat.java:1298)
    at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:1589)

2.

Caused by: java.lang.NumberFormatException: For input string: ".10201E.102014E4"
    at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1224)
    at java.lang.Double.parseDouble(Double.java:510)
    at java.text.DigitList.getDouble(DigitList.java:151)
    at java.text.DecimalFormat.parse(DecimalFormat.java:1303)
    at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:1589)

3.

Caused by: java.lang.NumberFormatException: multiple points
    at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1084)
    at java.lang.Double.parseDouble(Double.java:510)
    at java.text.DigitList.getDouble(DigitList.java:151)
    at java.text.DecimalFormat.parse(DecimalFormat.java:1303)
    at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:1936)
    at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1312)

结果不正确:

Sat Oct 22 00:00:00 BST 2011
Thu Jan 22 00:00:00 GMT 1970
Fri Oct 22 00:00:00 BST 2010
Fri Oct 22 00:00:00 BST 2010
Fri Oct 22 00:00:00 BST 2010
Thu Oct 22 00:00:00 GMT 1970
Fri Oct 22 00:00:00 BST 2010
Fri Oct 22 00:00:00 BST 2010
Fri Oct 22 00:00:00 BST 2010
Fri Oct 22 00:00:00 BST 2010

正确结果:

Fri Oct 22 00:00:00 BST 2010
Fri Oct 22 00:00:00 BST 2010
Fri Oct 22 00:00:00 BST 2010
Fri Oct 22 00:00:00 BST 2010
Fri Oct 22 00:00:00 BST 2010
Fri Oct 22 00:00:00 BST 2010
Fri Oct 22 00:00:00 BST 2010
Fri Oct 22 00:00:00 BST 2010
Fri Oct 22 00:00:00 BST 2010
Fri Oct 22 00:00:00 BST 2010

在多线程环境中安全使用 DateFormats 的另一种方法是使用 ThreadLocal 变量来保存 DateFormat 对象,这意味着每个线程都有自己的复制,不需要等待其他线程释放它。方法如下:

public class DateFormatTest {

  private static final ThreadLocal<DateFormat> df = new ThreadLocal<DateFormat>(){
    @Override
    protected DateFormat initialValue() {
        return new SimpleDateFormat("yyyyMMdd");
    }
  };

  public Date convert(String source) throws ParseException{
    Date d = df.get().parse(source);
    return d;
  }
}

这是一个很好的post有更多细节。

关于java - "Java DateFormat is not threadsafe"这会导致什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4021151/

相关文章:

java - 在 Java 中设置标志

java - 如何将方法作为参数传递

java - 如何删除磁盘上的 Redis 键?

java - HashMap 对于不同的键是线程安全的吗?

c++ - 对于 C++ MacOSX 应用程序,要使用什么线程库?

PHP 日期格式() : How to format date from string value

Java 正则表达式验证

javascript - 错误后多线程中的 Electron 进程未关闭

android - 如何在 Android 的日期格式中使用字符 'd'

Javascript 格式化日期很奇怪