SimpleDateFormat的线程安全问题

0

今天数据库保存的时间有错乱了,以前我以为是服务器的时间有问题,今天才发现原来是SimpleDateFormat这个类的format方法不是线程安全的,里面使用的calendar是个类变量,所以导致多线程静态对象出现了数据错乱或以下的一些异常:

Exception in thread "pool-1-thread-123" java.lang.NumberFormatException: For input string: ""
	at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
	at java.lang.Long.parseLong(Long.java:601)
	at java.lang.Long.parseLong(Long.java:631)
	at java.text.DigitList.getLong(DigitList.java:195)
	at java.text.DecimalFormat.parse(DecimalFormat.java:2051)
	at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:2160)
	at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1514)
	at java.text.DateFormat.parse(DateFormat.java:364)
	at com.demo.time.Format$1.run(Format.java:25)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at java.lang.Thread.run(Thread.java:745)
Exception in thread "pool-1-thread-314" java.lang.NumberFormatException: multiple points
	at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1890)
	at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
	at java.lang.Double.parseDouble(Double.java:538)
	at java.text.DigitList.getDouble(DigitList.java:169)
	at java.text.DecimalFormat.parse(DecimalFormat.java:2056)
	at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:1867)
	at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1514)
	at java.text.DateFormat.parse(DateFormat.java:364)
	at com.demo.time.Format$1.run(Format.java:25)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at java.lang.Thread.run(Thread.java:745)

解决办法:

  1. 使用局部变量SimpleDateFormat
  2. 使用ThreadLocal
  3. 使用synchronized
  4. 改用其他转换工具类org.apache.commons.lang.time.DateUtils.parseDate

验证代码:

package com.acgist.date;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Format {

	public static final SimpleDateFormat FORMATER = new SimpleDateFormat("yyyyMMddHHmmss");

	public static final ThreadLocal<DateFormat> FORMAT_FACTORY = new ThreadLocal<DateFormat>() {
		@Override
		protected DateFormat initialValue() {
			return new SimpleDateFormat("yyyyMMddHHmmss");
		}
	};
	
	public static void main(String[] args) throws InterruptedException {
		int count = 10000;
		ExecutorService pool = Executors.newFixedThreadPool(100);
		CountDownLatch latch = new CountDownLatch(count);
		for (int i = 0; i < count; i++) {
			pool.execute(new Runnable() {
				@Override
				public void run() {
					try {
						FORMAT_FACTORY.get().parse("20171212121212");
						FORMAT_FACTORY.remove();
//						FORMATER.parse("20171212121212");
					} catch (ParseException e) {
						System.err.println("发生异常:" + e.getMessage());
					} finally {
						latch.countDown();
					}
				}
			});
		}
		latch.await();
		pool.shutdown();
		System.out.println("执行完成");
//		Thread.sleep(10L * 60 * 1000);
	}

}

参考文章:http://www.cnblogs.com/peida/archive/2013/05/31/3070790.html