欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SimpleDateFormat线程不安全

程序员文章站 2024-03-15 16:36:42
...

现状

项目中出现一个现象,在格式化时间的时候出现ArrayIndexOutofBoundsException错误。

然后发现SimpleDateFormat是线程不安全的,除非是作为局部变量,被回收掉。

但是项目中是作为一个Util类的全局变量,而且是static的,导致多个servlet共用一个simpleDateFormat。

参考

https://blog.csdn.net/csdn_ds/article/details/72984646

参考如上代码然后进行试验,果然会出现数组越界异常,

 

SimpleDateFormat线程不安全

public class SimpleDateFormateTest2 {



    static SafeSimpleDateFormat safeSdf = new SafeSimpleDateFormat("yyyyMMdd,HHmmss");



    public static void main(String[] args) {



        final DateFormat df = new SimpleDateFormat("yyyyMMdd,HHmmss");

        ExecutorService ts = Executors.newFixedThreadPool(100);

        for (int i=0;i<10000;i++) {

            ts.execute(new Runnable() {

                @Override

                public void run() {

                    try {

                        //生成随机数,格式化日期

                        String format =  safeSdf.format(new Date(Math.abs(new Random().nextLong())));

                        System.out.println(format);

                    } catch (Exception e) {

                        e.printStackTrace();

                        System.exit(1);

                    }

                }

            });

        }

    }

}

然后根据试验了下parse方法,在100次循环内出现了异常,异常有很多展示。

 

 

SimpleDateFormat线程不安全

SimpleDateFormat线程不安全

SimpleDateFormat线程不安全

SimpleDateFormat线程不安全

试验代码如下

import com.fable.common.util.SafeSimpleDateFormat;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author lw
 * @date 2019/10/15 0015
 * @description
 */
public class SimpleDateFormateTest extends Thread {

    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    private static SafeSimpleDateFormat safeSdf = new SafeSimpleDateFormat("yyyy-MM-dd");

    private String name;
    private String dateStr;

    public SimpleDateFormateTest(String name, String dateStr) {
        this.name = name;
        this.dateStr = dateStr;
    }

    @Override
    public void run() {

        try {
            Date date = sdf.parse(dateStr);
            System.out.println(name + ": date:" + date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    public static void test() {
        ExecutorService executorService = Executors.newFixedThreadPool(3);

        executorService.execute(new SimpleDateFormateTest("A", "2017-06-10"));
        executorService.execute(new SimpleDateFormateTest("B", "2016-06-06"));
        executorService.shutdown();
    }

    public static void main(String[] args) {
        for (int i = 0; i < 100; i++)
            SimpleDateFormateTest.test();
    }
}

解决

此处使用网页中推荐的第四种方案,写ThreadLocal.

import org.springframework.util.Assert;

import java.text.DateFormatSymbols;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;

/**
 * This class implements a Thread-Safe (re-entrant) SimpleDateFormat
 * class.  It does this by using a ThreadLocal that holds a Map, instead
 * of the traditional approach to hold the SimpleDateFormat in a ThreadLocal.
 * <p>
 * Each ThreadLocal holds a single HashMap containing SimpleDateFormats, keyed
 * by a String format (e.g. "yyyy/M/d", etc.), for each new SimpleDateFormat
 * instance that was created within the threads execution context.
 *
 * @author John DeRegnaucourt (aaa@qq.com)
 *         <br/>
 *         Copyright (c) John DeRegnaucourt
 *         <br/><br/>
 *         Licensed under the Apache License, Version 2.0 (the "License");
 *         you may not use this file except in compliance with the License.
 *         You may obtain a copy of the License at
 *         <br/><br/>
 *         http://www.apache.org/licenses/LICENSE-2.0
 *         <br/><br/>
 *         Unless required by applicable law or agreed to in writing, software
 *         distributed under the License is distributed on an "AS IS" BASIS,
 *         WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *         See the License for the specific language governing permissions and
 *         limitations under the License.
 */
public class SafeSimpleDateFormat {
	private final String _format;

	private static final ThreadLocal<Map<String, SimpleDateFormat>> _dateFormats = new ThreadLocal<Map<String, SimpleDateFormat>>() {
		public Map<String, SimpleDateFormat> initialValue() {
			return new HashMap<String, SimpleDateFormat>();
		}
	};

	private SimpleDateFormat getDateFormat(String format) {
		Map<String, SimpleDateFormat> formatters = _dateFormats.get();
		SimpleDateFormat formatter = formatters.get(format);
		if (formatter == null) {
			formatter = new SimpleDateFormat(format);
			formatters.put(format, formatter);
		}
		return formatter;
	}

	public SafeSimpleDateFormat(String format) {
		Assert.notNull(format);
		_format = format;
	}

	public String format(Date date) {
		return getDateFormat(_format).format(date);
	}

	public String format(Object date) {
		return getDateFormat(_format).format(date);
	}

	public Date parse(String day) throws ParseException {
		return getDateFormat(_format).parse(day);
	}

	public void setTimeZone(TimeZone tz) {
		getDateFormat(_format).setTimeZone(tz);
	}

	public void setCalendar(Calendar cal) {
		getDateFormat(_format).setCalendar(cal);
	}

	public void setNumberFormat(NumberFormat format) {
		getDateFormat(_format).setNumberFormat(format);
	}

	public void setLenient(boolean lenient) {
		getDateFormat(_format).setLenient(lenient);
	}

	public void setDateFormatSymbols(DateFormatSymbols symbols) {
		getDateFormat(_format).setDateFormatSymbols(symbols);
	}

	public void set2DigitYearStart(Date date) {
		getDateFormat(_format).set2DigitYearStart(date);
	}
}