java8 - 多线程时间安全问题

 

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class TestSimpleDateFormat {
    
    public static void main(String[] args) throws Exception {
        
        /*SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        
        Callable<Date> task = new Callable<Date>() {

            @Override
            public Date call() throws Exception {
                return sdf.parse("20161121");
            }
            
        };

        ExecutorService pool = Executors.newFixedThreadPool(10);
        
        List<Future<Date>> results = new ArrayList<>();
        
        for (int i = 0; i < 10; i++) {
            results.add(pool.submit(task));
        }
        
        for (Future<Date> future : results) {
            System.out.println(future.get());
        }
        
        pool.shutdown();*/
        
        //解决多线程安全问题
        /*Callable<Date> task = new Callable<Date>() {

            @Override
            public Date call() throws Exception {
                return DateFormatThreadLocal.convert("20161121");
            }
            
        };

        ExecutorService pool = Executors.newFixedThreadPool(10);
        
        List<Future<Date>> results = new ArrayList<>();
        
        for (int i = 0; i < 10; i++) {
            results.add(pool.submit(task));
        }
        
        for (Future<Date> future : results) {
            System.out.println(future.get());
        }
        
        pool.shutdown();*/
        
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMdd");
        
        Callable<LocalDate> task = new Callable<LocalDate>() {

            @Override
            public LocalDate call() throws Exception {
                LocalDate ld = LocalDate.parse("20161121", dtf);
                return ld;
            }
            
        };

        ExecutorService pool = Executors.newFixedThreadPool(10);
        
        List<Future<LocalDate>> results = new ArrayList<>();
        
        for (int i = 0; i < 10; i++) {
            results.add(pool.submit(task));
        }
        
        for (Future<LocalDate> future : results) {
            System.out.println(future.get());
        }
        
        pool.shutdown();
    }

}

 

DateFormatThreadLocal 

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateFormatThreadLocal {
    
    private static final ThreadLocal<DateFormat> df = new ThreadLocal<DateFormat>(){
        
        protected DateFormat initialValue(){
            return new SimpleDateFormat("yyyyMMdd");
        }
        
    };
    
    public static final Date convert(String source) throws ParseException{
        return df.get().parse(source);
    }

}

 

posted @ 2019-03-05 14:28  濤叔  阅读(364)  评论(0编辑  收藏  举报