package com.winston.test;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* SimpleDateFormat 非线程安全
* @Description:
* @Author Winston
* @Version 1.0 2018年3月1日 下午3:57:51
*/
public class TestSimpleDateFormat {
public static void main (String[] args) {
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
for (int i = 1; i < 1000; i++) {
new Thread(new Runnable() {
public void run () {
try {
Thread.sleep((int)(Math.random()*5)*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Date date = new Date();
String aa = sdf.format(date);
String bb =sdf.format(date);
if(!aa.equals(bb)){
System.out.println(aa+"||"+bb);
}
}
}).start();
}
}
}
输出:
2018-03-01 15:57:30:867||2018-03-01 15:57:30:871
2018-03-01 15:57:31:786||2018-03-01 15:57:31:788
2018-03-01 15:57:34:875||2018-03-01 15:57:34:874