Import Java. util. arraylist; import Java. util. list; import Java. util. concurrent. callable; import Java. util. concurrent. executorservice; import Java. util. concurrent. executors; import Java. util. concurrent. future;/***** @ author caozhongping ** asynchronous Task Processing */public class asynctaskexecutor <v> {private callable <v> callable; private int threadsize; Private Static executorservice service; public asynctaskexecutor (call Able <v> callable, int threadsize) {super (); this. callable = callable; this. threadsize = threadsize;}/*** start task return progress * @ return */public list <future <v> Start () {service = executors. newfixedthreadpool (threadsize); List <future <v> List = new arraylist <future <v> (threadsize); For (INT I = 0; I <threadsize; I ++) {list. add (service. submit (callable);} return list;} public executorservice getservice () {return s Ervice; }}// guiimport Java. AWT. borderlayout; import Java. AWT. color; import Java. AWT. event. actionevent; import Java. AWT. event. actionlistener; import Java. util. random; import Java. util. concurrent. callable; import Java. util. concurrent. executionexception; import Java. util. concurrent. future; import javax. swing. box; import javax. swing. jbutton; import javax. swing. jframe; import javax. swing. jlabel; import javax. swing. Jprogressbar; import javax. swing. jscrollpane; import javax. swing. jtable; import javax. swing. jtextarea; import javax. swing. jtextfield; import javax. swing. table. defaulttablemodel; public class Client extends jframe {Private Static final long serialversionuid = 1l; private jtable table; private jlabel label; private jtextfield field; private jbutton button; private jtextarea status; private jprogressbar bar; Private jlabel label2; Public client (INT width, int height) {table = new jtable (New defaulttablemodel (); label = new jlabel ("name "); field = new jtextfield (15); button = new jbutton (""); status = new jtextarea (10, 5); label2 = new jlabel ("progress"); status. setforeground (color. blue); bar = new jprogressbar (); box = Box. createhorizontalbox (); box. add (box. createhorizontalstrut (50); box. add (Label ); Box. add (field); box. add (button); box. add (box. createhorizontalstrut (50); this. setlayout (New borderlayout (); this. add (box, borderlayout. north); this. add (table, borderlayout. center); this. add (New jscrollpane (Status), borderlayout. south); this. setsize (width, height); this. button. addactionlistener (New actionlistener () {@ overridepublic void actionreceivmed (actionevent arg0) {try {long St = system. current Timemillis (); asynctaskexecutor <long> executor = new asynctaskexecutor <long> (New mytask (1000, status), 5); long res = 0; button. setenabled (false); For (Future <long> future: executor. start () {If (! Future. isdone () {long temp = Future. get (). longvalue (); Res + = temp;} executor. getservice (). shutdown (); status. append ("\ n execution completed:" + Res + "Time consumed:" + (system. currenttimemillis ()-St); button. setenabled (true);} catch (exception e) {e. printstacktrace () ;}});} public defaulttablemodel GetModel () {return (defaulttablemodel) This. table. getModel ();} public static void main (string [] ARGs) {Client client = new client (300,400); client. setdefaclocloseoperation (client. exit_on_close); client. setlocationbyplatform (true); client. setvisible (true);} class mytask implements callable <long> {private int size; private jtextarea area; public mytask (INT size, jtextarea area) {super (); this. size = size; this. area = Area ;}@ overridepublic long call () throws exception {long sum = 0; For (INT I = 0; I <= size; I ++) {sum + = I; area. append ("\ n %" + sum) ;}return sum ;}}}