首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

SimpleDateFormat 线程安全的有关问题

2012-08-28 
SimpleDateFormat线程安全的问题SimpleDateFormat中的日期格式不是同步的。推荐(建议)为每个线程创建独立的

SimpleDateFormat 线程安全的问题

SimpleDateFormat中的日期格式不是同步的。推荐(建议)为每个线程创建独立的格式实例。如果多个线程同时访问一个格式,则它必须保持外部同步

?

JDK原始文档如下:

Synchronization

?

Date formats are not synchronized. It is recommended to create separate format instances for each thread. If multiple threads access a format concurrently, it must be synchronized externally.

?

?

如下内容摘自:http://www.111cn.net/jsp/Java/38190.htm

在java项目中,我们通常会自己写一个dateutil类,处理日期和字符串的转换。如下

public class dateutil {  private static final string date_format = "yyyymmdd";  @suppresswarnings("rawtypes") private static threadlocal threadlocal = new threadlocal() {  protected synchronized object initialvalue() {   return new simpledateformat(date_format);  } };  public static dateformat getdateformat() {  return (dateformat) threadlocal.get(); }  public static date parse(string textdate) throws parseexception {  return getdateformat().parse(textdate); }}
?

热点排行