ThreadLocalDemo

package com.fh.interview;

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

/**
 * @author
 * @create 2018-06-03 上午11:15
 **/
public class ThreadLocalDemo {

    private static ThreadLocal<SimpleDateFormat> sdf = new ThreadLocal<SimpleDateFormat>();

    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        for (int i=0;i<100;i++){
            executorService.submit(new DateUtils("2019-11-25 09:00:" + i % 60));
        }

    }

    static class DateUtils implements Runnable{

        private String date;

        private DateUtils(String date){
            this.date = date;
        }
        @Override
        public void run() {
            if(sdf.get() == null){
                sdf.set(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
            }else {
                SimpleDateFormat simpleDateFormat = sdf.get();
                try {
                    Date dates = simpleDateFormat.parse(date);
                    System.out.println(dates);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }



}
原文地址:https://www.cnblogs.com/nihaofenghao/p/9129353.html