Threadpooltaskexecutor multi-threaded decomposition performs a simple comparison of time-consuming tasks with single-threaded serial execution

Source: Internet
Author: User


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&nbsp ...");        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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.