欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java多线程编程中使用DateFormat类

程序员文章站 2024-03-04 16:45:29
 dateformat 类是一个非线程安全的类。javadocs 文档里面提到"date formats是不能同步的。 我们建议为每个线程创建独立的日期格式。 如...

 dateformat 类是一个非线程安全的类。javadocs 文档里面提到"date formats是不能同步的。 我们建议为每个线程创建独立的日期格式。 如果多个线程同时访问一个日期格式,这需要在外部加上同步代码块。"

以下的代码为我们展示了如何在一个线程环境里面使用dateformat把字符串日期转换为日期对象。创建一个实例来获取日期格式会比较高效,因为系统不需要多次获取本地语言和国家。
 

public class dateformattest {
 
 private final dateformat format =
      new simpledateformat("yyyymmdd");
 
 public date convert(string source)
           throws parseexception{
  date d = format.parse(source);
  return d;
 }
}

这段代码是非线程安全的。我们可以通过在多个线程中调用它。在以下调用的代码中,我创建了一个有两个线程的线程池,并提交了5个日期转换任务,之后查看运行结果:
 

final dateformattest t =new dateformattest();
callable<date> task =new callable<date>(){
  public date call()throws exception {
    return t.convert("20100811");
  }
};
 
//让我们尝试2个线程的情况
executorservice exec = executors.newfixedthreadpool(2);
list<future<date>> results =
       new arraylist<future<date>>();
 
//实现5次日期转换
for(int i =0; i <5; i++){
  results.add(exec.submit(task));
}
exec.shutdown();
 
//查看结果
for(future<date> result : results){
  system.out.println(result.get());
}

代码的运行结果并非如我们所愿 - 有时候,它输出正确的日期,有时候会输出错误的(例如.sat jul 31 00:00:00 bst 2012),有些时候甚至会抛出numberformatexception!


如何并发使用dateformat类

我们可以有多种方法在线程安全的情况下使用dateformat类。

1. 同步

最简单的方法就是在做日期转换之前,为dateformat对象加锁。这种方法使得一次只能让一个线程访问dateformat对象,而其他线程只能等待。
 

public date convert(string source)
          throws parseexception{
 synchronized(format) {
  date d = format.parse(source);
  return d;
 }
}


2. 使用threadlocal

另外一个方法就是使用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;
 }
}

3. joda-time

joda-time 是一个很棒的开源的 jdk 的日期和日历 api 的替代品,其 datetimeformat 是线程安全而且不变的。

 

import org.joda.time.datetime;
import org.joda.time.format.datetimeformat;
import org.joda.time.format.datetimeformatter;
import java.util.date;
 
public class dateformattest {
 
 private final datetimeformatter fmt =
    datetimeformat.forpattern("yyyymmdd");
 
 public date convert(string source){
  datetime d = fmt.parsedatetime(source);
  returnd.todate();
 }
}