Java executes the task asynchronously and returns the progress

Source: Internet
Author: User
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 ;}}}

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.