Import java.util.arraylist;import java.util.list;import java.util.concurrent.callable;import java.util.concurrent.ExecutionException;import java.util.concurrent.Future;import java.util.concurrent.threadpoolexecutor;import java.util.concurrent.timeunit;import org.apache.commons.lang.stringutils;import org.apache.commons.lang.exception.exceptionutils;import org.springframework.scheduling.concurrent.threadpooltaskexecutor;/** * <pre> * * @author tangxiaodong * Date Created: 2014 November 18 * </pre> */public class TaskTest { /** * @param args * @throws executionexception * @throws interruptedexception */ public static void main (String[] args) throws interruptedexception, executionexception { system.out.println ("TESTNOTHREADTASK&NBSP;BEGIN&NBSP; ..."); testnothreadtask (); fdfcutil.sleep (1); system.out.println ("TESTTHREADTASK&NBSP;BEGIN&NBSP; ..."); testthreadtask (); } Private static void testnothreadtask () { Long t1 = system.currenttimemillis (); for ( int k = 0; k < 10; k++) { // This is a time-consuming work fdfcutil.sleep (k + 1) * 500); System.out.println (" k=" + k); system.out.println ("k=" + k + " success"); } system.out.println ("&NBSP;TIMES&NBSP;:"); system.out.println (System.currenttimemillis () - t1); } private Static void testthreadtask () { // TODO Auto-generated method stub threadpoOltaskexecutor executor = new threadpooltaskexecutor (); executor.setcorepoolsize (2); Executor.setmaxpoolsize (4); executor.setkeepaliveseconds (120); executor.setqueuecapacity (+); executor.setrejectedexecutionhandler (New threadpoolexecutor.callerrunspolicy ()); executor.initialize (); list<future<string>> taskresults = new ArrayList<Future<String>> ( long); t1 = system.currenttimemillis (); for (int k =0; k<10; k++) {&NBSP;&NBSP;&NBsp; taskresults.add (Process (executor, k)); } system.out.println ( "Task doing  ..."); while (True) { boolean isAllDone = true; for (future<string> Taskresult : taskresults) { isAllDone &= ( taskresult.isdone () | | taskresult.iscancelled () ); } if (IsAllDone) { // tasks are completed, jump out of the loop break; } try { system.out.println (" WAITING&NBSP;AND&NBSP;SLEEP&NBSP;1000&NBSP, ... "); timeunit.milliseconds.sleep (; ) } catch (exception e) { system.out.println (E.toString ()); break; } } for (Future<String> Taskresult : taskresults) { string ex; try { ex = Taskresult.get (); } catch ( Exception e) { ex = exceptionutils.getfullstacktrace (e); } if ( Stringutils.isnotempty (ex)) { &nbsP; system.out.println (ex); } } system.out.println ("&NBSP;TIMES&NBSP;:"); system.out.println (System.currenttimemillis ()-t1); } private static future<string> process (final threadpooltaskexecutor executor , final int k) { return executor.submit (new callable<string> () { @Override public string call () throws Exception { try { fdfcutil.sleep ((k+1) *500); system.out.println (" k= " + k); } catch (exception e) { return exceptionutils.getfullstacktrace (e); } return "k=" + k + " success"; } }); }}
Testnothreadtask Time:
times:27996 ms
Testthreadtask Time:
times:15239 ms
Threadpooltaskexecutor multi-threaded decomposition performs a simple comparison of time-consuming tasks with single-threaded serial execution