SimpleDateFormat线程不安全

现状

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

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

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

参考

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

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

 

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次循环内出现了异常,异常有很多展示。

 

 

试验代码如下

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 (jdereg@gmail.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);
	}
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值