Java_invokeall_ return Value _ multiple threads executing simultaneously, canceling timeout thread

Source: Internet
Author: User

 PackageCom.demo.test4;Importjava.util.ArrayList;Importjava.util.List;Importjava.util.concurrent.Callable;Importjava.util.concurrent.CancellationException;Importjava.util.concurrent.ExecutionException;ImportJava.util.concurrent.ExecutorService;Importjava.util.concurrent.Executors;Importjava.util.concurrent.Future;ImportJava.util.concurrent.TimeUnit;/** * @authorqq:1236897 Cancel Timeout thread if limit time is exceeded **/ Public classInvokedemo {/**     * @paramargs*/     Public Static voidMain (string[] args) {//TODO auto-generated Method StubExecutorservice exec =Executors.newcachedthreadpool (); List<SearchTask> Searchtasks =NewArraylist<searchtask>(); Searchtask St=NULL;  for(inti = 0; I < 10; i++) {St=NewSearchtask (i, 200);//specify 2001 mm for maximum execution time            if(i = = 5) St=NewSearchtask (i, 2001);//specify 2001 mm for maximum execution timeSearchtasks.add (ST); }        Try {            //requires that the result be returned within 2000 milliseconds, otherwise the execution is canceled. List<future<list<person>>> futures =Exec.invokeall (Searchtasks,, timeunit.milliseconds);//InvokeAll//The first parameter is the task list, the second parameter is the expiration time, and the third is the expiration unit            intCount = 0;  for(future<list<person>>future:futures) {                Try{List<Person> students =Future.get ();  for(person student:students) {System.out.println (student.tostring ()); }                } Catch(cancellationexception e) {System.out.println ("Cancel"); Future.cancel (true); } System.out.println ("-----------------------" +Count+ "--------------------"); Count++;        } exec.shutdown (); } Catch(interruptedexception e) {e.printstacktrace ();        Thread.interrupted (); } Catch(executionexception e) {e.printstacktrace (); }    }}/*** @filename: Searchtask * @description: Query task *@authorLida * @date 2013-4-1 pm 3:02:29*/classSearchtaskImplementsCallable<list<person>> {     Public Final intClassID;  Public LongSleeptime; /*** <p> * Title: * </p> * <p> * Description: * </p> * *@paramClassID * Class number *@paramsleeptime * Number of hours spent in analog operations (MS)*/Searchtask (intClassID,Longsleeptime) {         This. ClassID =ClassID;  This. Sleeptime =Sleeptime; } @Override PublicList<person> Call ()throwsException {//the number of hours that the simulation operation took (milliseconds)Thread.Sleep (sleeptime); List<Person> stulist =NewArraylist<person>(); Person P=NewPerson (1, "Name", 2); Person P2=NewPerson (2, "name", 3);        Stulist.add (P);        Stulist.add (p2); returnstulist; }}classPerson {Private intID; PrivateString name; Private intClassID;  PublicPerson (intID, String name,intClassID) {         This. ID =ID;  This. Name =name;  This. ClassID =ClassID; }     PublicString toString () {returnPerson.class. GetName () + "(ID:" + This. ID + ", Name:" + This. Name+ ")"; }}

Java_invokeall_ return Value _ multiple threads executing simultaneously, canceling timeout thread

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.