Java Concurrency Programming: thread Pool-instance

Source: Internet
Author: User

Code block:
1  Public classTest {2      Public Static voidMain (string[] args) {3Test T =Newtest ();4Threadpoolexecutor executor =NewThreadpoolexecutor (5, 10, 200,5Timeunit.milliseconds,NewLinkedblockingdeque<runnable> (5));6 7          for(inti = 1; I < 16; i++) {8 T.testrun (executor, i);9System.out.println ("--Number of threads in thread pool:" + executor.getpoolsize () +Ten"--Number of threads in the column:" + executor.getqueue (). Size () + One"--Number of threads executed:" +Executor.getcompletedtaskcount ()); A         } - Executor.shutdown (); -          while(true) { the             if(executor.isterminated ()) { -SYSTEM.OUT.PRINTLN ("*------* All threads have ended"); -                  Break; -             } +             Try { -Thread.Sleep (100); +}Catch(interruptedexception e) { A e.printstacktrace (); at             } -         } -  -     } -  -      Public voidTestRun (Threadpoolexecutor executor,Final inta) { in  -Executor.execute (NewThread (NewRunnable () { to @Override +              Public voidrun () { -SYSTEM.OUT.PRINTLN ("Thread Start, Thread:" +a); the                 Try { *Thread.Sleep (A * 1000); $}Catch(interruptedexception e) {Panax Notoginseng e.printstacktrace (); -                 } theSystem.out.println ("Thread ends, Thread:" +a); +             } A}, "name")); the  +     } -}
Operation Result:
Thread Start, Thread: Number of threads in the thread pool: 1--Number of threads in the column: 0--Number of threads executed: 0--thread pool: 2--Number of threads in the column: 0--Number of threads executed: 0thread Start, Thread:2--threads in thread pool: 3--Number of threads in the column: 0--Number of threads executed: 0thread Start, Thread:3--threads in thread pool: 4--Number of threads in the column: 0--Number of threads executed: 0thread Start, Thread:4--threads in thread pool: 5--Number of threads in the column: 0--Number of threads executed: 0thread Start, Thread:Number of threads in the 5--thread pool: 5--Number of threads in the column: 1--Number of threads executed: 0--thread pool: 5--Number of threads in the column: 2--Number of threads executed: 0--threads in the thread pool: 5--Number of threads in the column: 3--executed  Number of threads completed: Number of threads in 0--thread pool: 5--Number of threads in the column: 4--Number of threads executed: 0--thread pool: 5--Number of threads in the column: 5--Number of threads executed: 0--threads in thread pool: 6--Number of threads in the column: 5 --Number of threads executed: 0thread Start, Thread:11--threads in thread pool: 7--Number of threads in the column: 5--Number of threads executed: 0thread Start, Thread:12--threads in thread pool: 8--Number of threads in the column: 5--Number of threads executed: 0thread Start, Thread:13--threads in thread pool: 9--Number of threads in the column: 5--Number of threads executed: 0thread Start, Thread:14--threads in thread pool: 10--Number of threads in the column: 5--Number of threads executed: 0thread Start, Thread:15Thread end, Thread:1thread Start, Thread:6Thread end, Thread:2thread Start, Thread:7Thread end, Thread:3thread Start, Thread:8Thread end, Thread:4thread Start, Thread:9Thread end, Thread:5thread Start, Thread:10Thread end, Thread:6Thread end, Thread:7Thread end, Thread:11Thread end, Thread:8Thread end, Thread:12Thread end, Thread:13Thread end, Thread:9Thread end, Thread:14Thread end, Thread:15Thread end, Thread:10*------* All threads have ended
View Code

Java Concurrency Programming: thread Pool-instance

Related Article

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.