Java多线程编程中使用DateFormat类
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();
}
}
相关文章
Java-URLDecoder、URLEncoder使用及说明
本文介绍了Java中URLDecoder和URLEncoder类的使用方法,包括编码和解码规则、推荐的编码方案、解码器处理非法字符的方法以及URL编码和解码的示例2024-12-12
java、python、JavaScript以及jquery循环语句的区别
本篇文章主要介绍java、python、JavaScript以及jquery的循环语句的区别,这里整理了它们循环语句语法跟示例,以便大家阅读,更好的区分它们的不同2016-07-07
springboot打包不同环境配置以及shell脚本部署的方法
这篇文章主要给大家介绍了关于springboot打包不同环境配置以及shell脚本部署的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者使用springboot具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧2019-03-03
Java实现将Object转换成指定Class对象的操作代码
这篇文章主要介绍了Java实现将Object转换成指定Class对象的操作,在Java中,将Object转换为指定类型的Class对象实际上是两个不同概念的操作,由于你提到的“将Object转换成指定Class对象”可能有些混淆,我将分别展示这两种操作的示例代码,需要的朋友可以参考下2024-09-09


最新评论