相关疑难解决方法(0)

同步对SimpleDateFormat的访问

SimpleDateFormat的javadoc声明SimpleDateFormat未同步.

"日期格式不同步.建议为每个线程创建单独的格式实例.如果多个线程同时访问格式,则必须在外部同步."

但是在多线程环境中使用SimpleDateFormat实例的最佳方法是什么.以下是我想到的一些选项,我过去使用过选项1和2,但我很想知道是否有更好的替代方案或哪些选项可以提供最佳性能和并发性.

选项1:在需要时创建本地实例

public String formatDate(Date d) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    return sdf.format(d);
}
Run Code Online (Sandbox Code Playgroud)

选项2:将SimpleDateFormat的实例创建为类变量,但同步对其的访问.

private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
public String formatDate(Date d) {
    synchronized(sdf) {
        return sdf.format(d);
    }
}
Run Code Online (Sandbox Code Playgroud)

选项3:创建ThreadLocal以为每个线程存储SimpleDateFormat的不同实例.

private ThreadLocal<SimpleDateFormat> tl = new ThreadLocal<SimpleDateFormat>();
public String formatDate(Date d) {
    SimpleDateFormat sdf = tl.get();
    if(sdf == null) {
        sdf = new SimpleDateFormat("yyyy-MM-hh");
        tl.set(sdf);
    }
    return sdf.format(d);
}
Run Code Online (Sandbox Code Playgroud)

java concurrency multithreading simpledateformat

86
推荐指数
5
解决办法
2万
查看次数