1,SimpleDateFormat不是线程安全的
public class SimpleDateFormatTest {
static SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyyMMddHHmmss");
public static void main(String[] args) {
for(int i=0;i<100;i++) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
System.out.println(simpleDateFormat.parse("20210420111111"));
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
}
}
}
上面的例子会出现 NumberFormatException 异常:
2,解决办法
(1)对象加锁
public class