There is a thread safety issue below:
Public classTestsimpledateformat { Public Static voidMain (string[] args)throwsinterruptedexception, executionexception {//SimpleDateFormat sdf = new SimpleDateFormat ("YyyyMMdd");DateTimeFormatter DTF= Datetimeformatter.ofpattern ("YyyyMMdd");//Java 8 availablecallable<LocalDate> task =NewCallable<localdate>() {@Override PublicLocaldate Call ()throwsException {returnLocaldate.parse ("20180109", DTF);//Java 8 available//return Dateformatthreadlocal.convert ("20180109"); } }; List<Future<LocalDate>> list =NewArraylist<>(); Executorservice Pool= Executors.newfixedthreadpool (10); for(inti=0;i<10;i++) {List.add (Pool.submit (Task)); } for(future<localdate>future:list) {System.out.println (Future.get ()); } pool.shutdown (); //Remember to close } }
Here is the Threadlocal class:
public class Dateformatthreadlocal { private static final threadlocal<dateformat> tl = new threadlocal< Dateformat> () { protected DateFormat InitialValue () { return new SimpleDateFormat ("YyyyMMdd"); } }; Public static final Date convert (String source) throws parseexception{ return Tl.get (). Parse (source);} }
[Technology Sharing]20180109_ Multithreading _ threadlocal Addressing thread safety issues (JDK 1.7)