Java SimpleDateFormat使用注意:不支持多线程中定义全局的(static)SimpleDateFormat

public class Test {
	
	private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static void main(String[] args) {
        ExecutorService service = Executors.newFixedThreadPool(100);
		for (int i = 0; i < 20; i++) {
	        service.execute(new Runnable() {
				
				@Override
				public void run() {
					 for (int j = 0; j < 10; j++) {
			                try {
			                    System.out.println(sdf.parse("2019-01-02 09:45:59"));
			                } catch (ParseException e) {
			                    e.printStackTrace();
			                }
			            }
					
				}
			});
	    }
	}
}

运行结果: 

Exception in thread "pool-1-thread-3" Exception in thread "pool-1-thread-5" java.lang.NumberFormatException: multiple points
	at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1101)
	at java.lang.Double.parseDouble(Double.java:540)
	at java.text.DigitList.getDouble(DigitList.java:168)
	at java.text.DecimalFormat.parse(DecimalFormat.java:1321)
	at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:2088)
	at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1455)
	at java.text.DateFormat.parse(DateFormat.java:355)
	at com.adolph.org.Test$1.run(Test.java:91)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
	at java.lang.Thread.run(Thread.java:744)
java.lang.NumberFormatException: multiple points
	at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1101)
	at java.lang.Double.parseDouble(Double.java:540)
	at java.text.DigitList.getDouble(DigitList.java:168)
	at java.text.DecimalFormat.parse(DecimalFormat.java:1321)
	at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:2088)
	at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1455)
	at java.text.DateFormat.parse(DateFormat.java:355)
	at com.adolph.org.Test$1.run(Test.java:91)Tue Jan 02 09:45:59 CST 2019
Tue Jan 02 09:45:59 CST 2019
Tue Jan 02 09:45:59 CST 2019
Tue Jan 02 09:45:59 CST 2019
Thu Dec 20 09:45:59 CST 2019

	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
	at java.lang.Thread.run(Thread.java:744)
Exception in thread "pool-1-thread-7" java.lang.NumberFormatException: For input string: "E.4250918E4"
	at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1241)
	at java.lang.Double.parseDouble(Double.java:540)
	at java.text.DigitList.getDouble(DigitList.java:168)
	at java.text.DecimalFormat.parse(DecimalFormat.java:1321)
	at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:1793)
	at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1455)
Mon Dec 20 09:45:59 CST 1
	at java.text.DateFormat.parse(DateFormat.java:355)
	at com.adolph.org.Test$1.run(Test.java:91)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
	at java.lang.Thread.run(Thread.java:744)

原因:SimpleDateFormat为线程不安全类型,不支持多线程使用同一个SimpleDateFormat对象

转载于:https://my.oschina.net/u/3680947/blog/3045492

你可能感兴趣的:(Java SimpleDateFormat使用注意:不支持多线程中定义全局的(static)SimpleDateFormat)