6, JAVA5 thread pool fixed size thread pool Newfixedthreadpool

Source: Internet
Author: User

1  PackageCom.yzl;2 3 ImportJava.util.Random;4 Importjava.util.concurrent.Callable;5 ImportJava.util.concurrent.ExecutorService;6 Importjava.util.concurrent.Executors;7 Importjava.util.concurrent.Future;8 ImportJava.util.concurrent.TimeUnit;9 Ten  Public classThreadPool { One      Public Static voidMain (string[] args) { A Fixedthreadpool (); -     } -      the     /** - * fixed-size thread pool -      *  - * At the same time can handle the "parameter" task, the extra task will be queued, when the processing of a one will immediately go to the queue to deal with the task.  +      */ -     Private Static voidFixedthreadpool () { +Executorservice es = Executors.newfixedthreadpool (2); A         //Join 5 Quests at          for(intI=1; i<5; i++){ -             Final intTask =i; -Es.submit (NewRunnable () { - @Override -                  Public voidrun () { -                      for(intJ=1; j<=2; J + +){ inSYSTEM.OUT.PRINTLN ("Now running is the first" "+ Task +" "Task"); -System.out.println (Thread.CurrentThread (). GetName () + "is work, now loop to" +j); to                         if(j==2){ +SYSTEM.OUT.PRINTLN ("task" + task + "Run Complete"); -                         } the                         Try { *Thread.Sleep (100); $}Catch(interruptedexception e) {Panax Notoginseng e.printstacktrace (); -                         } the                     } +                 } A             }); the         } +System.out.println ("5 runnable task submit complete!!")); -  $         //Add 5 callable Tasks, the task after the completion of a return value will be blocked, that is, the results of 5 tasks will not continue to go down $          for(intI=1; i<=5; i++){ -             Final intTask =i; -future<integer> future = Es.submit (NewCallable<integer>() { the @Override -                  PublicInteger Call ()throwsException {Wuyi                     Try { theThread.Sleep (100); -}Catch(interruptedexception e) { Wu e.printstacktrace (); -                     } AboutSYSTEM.OUT.PRINTLN ("Callable task" + task + "Run"); $                     return NewRandom (). Nextint (100); -                 } -             }); -              A             //If the comment takes the result of the code, it does not clog +             /*try { the System.out.println ("task" + i + "" Returned result: "+ future.get ()); - } catch (Exception e) { $ e.printstacktrace (); the             }*/ the         }     theSystem.out.println ("5 callable Task submit complete!!" +System.currenttimemillis ()); the         //Although the shutdown method does not actually stop the thread pool until all tasks have been completed, the method does not cause a blockage, which means that the next line of code runs immediately after the code is run - Es.shutdown (); inSystem.out.println ("Main program shutdown after exit!!" +System.currenttimemillis ()); the          the         //the direct termination thread pool of violence About         //Es.shutdownnow (); the          the         //The Awaittermination method is plug-in, and only when the thread pool is really stopped will the program continue to execute . the         Try { +Es.awaittermination (2, timeunit.seconds); -}Catch(interruptedexception e) { the e.printstacktrace ();Bayi         } the          theSystem.out.println ("main program awaittermination after exit!!" +System.currenttimemillis ());  -  }  -}

Program Run Result:

1 5 runnable Tasks submit complete !!2Now running is section "1the Task3Now running is section "2the Task4Pool-1-thread-1is work, now loop to 15Pool-1-thread-2is work, now loop to 16 5 Callable Task submit complete!! 14412958485267 after the main program shutdown quit!! 14412958485268Now running is section "2the Task9Pool-1-thread-2is work, now loop to 2TenTask "2the Run complete OneNow running is section "1the Task APool-1-thread-1is work, now loop to 2 -Task "1the Run complete -Now running is section "3the Task thePool-1-thread-2is work, now loop to 1 -Now running is section "4the Task -Pool-1-thread-1is work, now loop to 1 -Now running is section "3the Task +Pool-1-thread-2is work, now loop to 2 -Task "3the Run complete +Now running is section "4the Task APool-1-thread-1is work, now loop to 2 atTask "4the Run complete -Callable Task "2the Run -Callable Task "1the Run -Callable Task "4the Run -Callable Task "3the Run -Callable Task "5the Run in Main programawaitterminationafter exiting!! 1441295849228

6, JAVA5 thread pool fixed size thread pool Newfixedthreadpool

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.