@Slf4j
public class Test {
private static ExecutorService service;
static {
ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("thread-%d").build();
service = new ThreadPoolExecutor(50, 100, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<>(100), threadFactory);
}
private static final SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public static void main(String[] args) {
String oldDate = SIMPLE_DATE_FORMAT.format(new Date());
for (int i = 0; i < 100; i++) {
service.execute(()->{
Date parse = null;
try {
parse = SIMPLE_DATE_FORMAT.parse(oldDate);
} catch (ParseException e) {
e.printStackTrace();
}
String newDate = SIMPLE_DATE_FORMAT.format(parse);
//多次转换,查看是否相等
log.info(StringUtils.equals(oldDate, newDate) + "");
});
}
}
}
上述代码执行后出现大量空指针异常和false的结果。
证明SimpleDateFormat确实不是线程安全的。
网友评论