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

SimpleDateFormat 是是非非线程安全

2012-10-30 
SimpleDateFormat是非线程安全在使用SimpleDateFormat的经常会有一些错误的用法,例如如下方式:public clas

SimpleDateFormat 是非线程安全
在使用SimpleDateFormat的经常会有一些错误的用法,例如如下方式:
public class TestDateFormat{
     private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
     public void method1(){
         sdf.format(new Date());
     }
     public void method2(){
         sdf.format(new Date());
     }
)
为了渐少new 的次数而把SimpleDateFormat做成成员或者静态成员,但这样的做法是隐含着错误的,是不
安全的。如下给出证明:
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


public class TestDateFormat{
    private SimpleDateFormat sdf ;
    public static void main(String[] args) {
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
        Date date1 = new Date();
        Date date2 = new Date(date1.getTime()+1000*60*60*24);
        System.out.println(date1);
        System.out.println(date2);
        Thread thread1 = new Thread(new Thread1(sdf,date1));
        thread1.start();
        Thread thread2 = new Thread(new Thread2(sdf,date2));
        thread2.start();
    }
   
}
class Thread1 implements Runnable{
    private SimpleDateFormat sdf;
    private Date date;
    public Thread1(SimpleDateFormat sdf,Date date){
        this.sdf = sdf;
        this.date = date;
    }
    public void run() {
        for(;;){
            String strDate = sdf.format(date);
            if("2007-02-27".equals(strDate)){
                System.err.println("Error: date1="+strDate);
                System.exit(0);
            }
        }
    }
}
class Thread2 implements Runnable{
    private SimpleDateFormat sdf;
    private Date date;
    public Thread2(SimpleDateFormat sdf,Date date){
        this.sdf = sdf;
        this.date = date;
    }
    public void run() {
        for(;;){
            String strDate = sdf.format(date);
            if("2007-02-26".equals(strDate)){
                System.err.println("Error: date1="+strDate);
                System.exit(0);
            }
        }
    }
}
很快,基本几十次就会出现错误。错误原因:
因为SimpleDateFormat处理复杂,Jdk的实现中使用了成员变量来传递参数,这个就造成在多线程的时候
会出现错误。上边的程序证明了这个问题。

再看看SimpleDateFormat的Jdk 的Source,有这么一段注释,说明它不是线程安全的。
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

热点排行