SimpleDateFormat非线程安全问题
实现1000个线程的时间格式化
package SimpleDateFormat ; import java . text . SimpleDateFormat ; import java . util . Date ; import java . util . concurrent . LinkedBlockingDeque ; import java . util . concurrent . ThreadPoolExecutor ; import java . util . concurrent . TimeUnit ; /** * user:ypc; * date:2021-06-13; * time: 17:30; */ public class SimpleDateFormat1 { private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat ( "mm:ss" ); public static void main ( String [] args ) { ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor ( 10 , 10 , 100 , TimeUnit . MILLISECONDS , new LinkedBlockingDeque <>( 1000 ), new ThreadPoolExecutor . DiscardPolicy ()); for ( int i = 0 ; i < 1001 ; i ++) { int finalI = i ; threadPoolExecutor . submit ( new Runnable () { @Override public void run () { Date date = new Date ( finalI * 1000 ); myFormatTime ( date ); } }); } threadPoolExecutor . shutdown (); } private static void myFormatTime ( Date date ){ System . out . println ( simpleDateFormat . format ( date )); } }产生了线程不安全的问题
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did214574