Private Threadfactory threadfactory = new Threadfactorybuilder (). Setnameformat ("billservice-pool-%d"). Build ();
Private final Executorservice pool = new Threadpoolexecutor (20,0l, Timeunit.milliseconds,new linkedblockingqueue <Runnable> (threadfactory);
Parse an instance:
FinalCountdownlatch Countdownlatch =NewCountdownlatch (Billuploadhistory.size ());//thread Statistics for(Billupload billupload:billuploadhistory) {Pool.execute (NewRunnable () {@Override Public voidrun () {Paybillparser Parseservice=NULL; if(EnumTypeOfInt.PAY_CHANNEL_WECHAT.getValue (). Equals (Billupload.getpaychannel ())) {Par Seservice=NewWeixinpaybillparser (Billupload, Logid); } Else if(EnumTypeOfInt.PAY_CHANNEL_ALIPAY.getValue (). Equals (Billupload.getpaychannel ())) {Par Seservice=NewAlipaybillparser (Billupload, Logid); } if(Parseservice! =NULL) { Try{parseservice.parse (); } Catch(Exception e) {e.printstacktrace (); } finally{countdownlatch.countdown ();//thread minus 1} } } }); } Try{countdownlatch.await ();//block until counter is 0}Catch(interruptedexception e) {e.printstacktrace (); }
Create a thread pool (Java)