Java thread pool

Source: Internet
Author: User

Thread class:

1 classRunthread implements runnable{2 3     intCounter =0;4 @Override5      PublicSynchronizedvoidrun () {6 7         Try {8Thread.Sleep ( -);9}Catch(interruptedexception e) {Ten e.printstacktrace (); One         }     ASystem. out. println (Thread.CurrentThread (). GetName () +"Count:"+counter++); -     } -  the}

Execute function:

1      Public Static voidMain (string[] args) {2 3Threadpoolexecutor executor =NewThreadpoolexecutor (5,Ten, $, Timeunit.milliseconds,4                 NewArrayblockingqueue<runnable> (Ten));5         6          for(inti =0; I < the; i++) {7Runthread Runthread =NewRunthread ();8System. out. println ("thread pool Number of threads:"+executor.getpoolsize () +", the number of tasks waiting to be executed in the queue:"+9Executor.getqueue (). Size () +", the number of other tasks that have been performed:"+Executor.getcompletedtaskcount ());Ten             Try { One Executor.execute (runthread); A}Catch(Exception e) { - e.printstacktrace (); -             } the              -         } - Executor.shutdown (); -}

Operation Result:

Thread pool number of threads: 0, number of tasks waiting to be executed in queue: 0, performed the number of other tasks played: 0thread pool Number of threads:1, number of tasks waiting to be executed in queue: 0, performed the number of other tasks played: 0thread pool Number of threads:2, number of tasks waiting to be executed in queue: 0, performed the number of other tasks played: 0thread pool Number of threads:3, number of tasks waiting to be executed in queue: 0, performed the number of other tasks played: 0thread pool Number of threads:4, number of tasks waiting to be executed in queue: 0, performed the number of other tasks played: 0thread pool Number of threads:5, number of tasks waiting to be executed in queue: 0, performed the number of other tasks played: 0thread pool Number of threads:5, number of tasks waiting to be executed in queue: 1, performed the number of other tasks played: 0thread pool Number of threads:5, number of tasks waiting to be executed in queue: 2, performed the number of other tasks played: 0thread pool Number of threads:5, number of tasks waiting to be executed in queue: 3, performed the number of other tasks played: 0thread pool Number of threads:5, number of tasks waiting to be executed in queue: 4, performed the number of other tasks played: 0thread pool Number of threads:5, number of tasks waiting to be executed in queue: 5, performed the number of other tasks played: 0thread pool Number of threads:5, number of tasks waiting to be executed in queue: 6, performed the number of other tasks played: 0thread pool Number of threads:5, number of tasks waiting to be executed in queue: 7, performed the number of other tasks played: 0thread pool Number of threads:5, number of tasks waiting to be executed in queue: 8, performed the number of other tasks played: 0thread pool Number of threads:5, number of tasks waiting to be executed in queue: 9, performed the number of other tasks played: 0Pool-1-thread-2count:0Pool-1-thread-4count:0Pool-1-thread-1count:0Pool-1-thread-3count:0Pool-1-thread-5count:0Pool-1-thread-4count:0Pool-1-thread-2count:0Pool-1-thread-1count:0Pool-1-thread-3count:0Pool-1-thread-5count:0Pool-1-thread-2count:0Pool-1-thread-4count:0Pool-1-thread-1count:0Pool-1-thread-5count:0Pool-1-thread-3count:0

Modify Parameters:

New Threadpoolexecutor (5, ten, Timeunit.milliseconds,new, arrayblockingqueue<runnable> (5));

Output Result:

Thread pool number of threads: 0, number of tasks waiting to be executed in queue: 0, performed the number of other tasks played: 0thread pool Number of threads:1, number of tasks waiting to be executed in queue: 0, performed the number of other tasks played: 0thread pool Number of threads:2, number of tasks waiting to be executed in queue: 0, performed the number of other tasks played: 0thread pool Number of threads:3, number of tasks waiting to be executed in queue: 0, performed the number of other tasks played: 0thread pool Number of threads:4, number of tasks waiting to be executed in queue: 0, performed the number of other tasks played: 0thread pool Number of threads:5, number of tasks waiting to be executed in queue: 0, performed the number of other tasks played: 0thread pool Number of threads:5, number of tasks waiting to be executed in queue: 1, performed the number of other tasks played: 0thread pool Number of threads:5, number of tasks waiting to be executed in queue: 2, performed the number of other tasks played: 0thread pool Number of threads:5, number of tasks waiting to be executed in queue: 3, performed the number of other tasks played: 0thread pool Number of threads:5, number of tasks waiting to be executed in queue: 4, performed the number of other tasks played: 0thread pool Number of threads:5, number of tasks waiting to be executed in queue: 5, performed the number of other tasks played: 0thread pool Number of threads:6, number of tasks waiting to be executed in queue: 5, performed the number of other tasks played: 0thread pool Number of threads:7, number of tasks waiting to be executed in queue: 5, performed the number of other tasks played: 0thread pool Number of threads:8, number of tasks waiting to be executed in queue: 5, performed the number of other tasks played: 0thread pool Number of threads:9, number of tasks waiting to be executed in queue: 5, performed the number of other tasks played: 0Pool-1-thread-4count:0Pool-1-thread-2count:0Pool-1-thread-5count:0Pool-1-thread-3count:0Pool-1-thread-1count:0Pool-1-thread-10count:0Pool-1-thread-9count:0Pool-1-thread-8count:0Pool-1-thread-7count:0Pool-1-thread-6count:0Pool-1-thread-4count:0Pool-1-thread-3count:0Pool-1-thread-5count:0Pool-1-thread-2count:0Pool-1-thread-1count:0

Modify Parameters:

Threadpoolexecutor executor = new Threadpoolexecutor (5, 8, $, timeunit.milliseconds,new arrayblockingqueue< Runnable> (5));

Output Result:

Thread pool number of threads: 0, number of tasks waiting to be executed in queue: 0, performed the number of other tasks played: 0thread pool Number of threads:1, number of tasks waiting to be executed in queue: 0, performed the number of other tasks played: 0thread pool Number of threads:2, number of tasks waiting to be executed in queue: 0, performed the number of other tasks played: 0thread pool Number of threads:3, number of tasks waiting to be executed in queue: 0, performed the number of other tasks played: 0thread pool Number of threads:4, number of tasks waiting to be executed in queue: 0, performed the number of other tasks played: 0thread pool Number of threads:5, number of tasks waiting to be executed in queue: 0, performed the number of other tasks played: 0thread pool Number of threads:5, number of tasks waiting to be executed in queue: 1, performed the number of other tasks played: 0thread pool Number of threads:5, number of tasks waiting to be executed in queue: 2, performed the number of other tasks played: 0thread pool Number of threads:5, number of tasks waiting to be executed in queue: 3, performed the number of other tasks played: 0thread pool Number of threads:5, number of tasks waiting to be executed in queue: 4, performed the number of other tasks played: 0thread pool Number of threads:5, number of tasks waiting to be executed in queue: 5, performed the number of other tasks played: 0thread pool Number of threads:6, number of tasks waiting to be executed in queue: 5, performed the number of other tasks played: 0thread pool Number of threads:7, number of tasks waiting to be executed in queue: 5, performed the number of other tasks played: 0thread pool Number of threads:8, number of tasks waiting to be executed in queue: 5, performed the number of other tasks played: 0java.util.concurrent.RejectedExecutionException:Task [email protected]1884174 rejected from [email protected][running, pool size = 8, Active threads = 8, queued tasks = 5, completed tasks = 0] at java.util.concurrent.threadpoolexecutor$abortpolicy.rejectedexecution (Unknown Source) at Java.util.concurr Ent. Threadpoolexecutor.reject (Unknown source) at Java.util.concurrent.ThreadPoolExecutor.execute (Unknown source) at COM. Uestc.xst.Main.main (Main.java:73) Java.util.concurrent.RejectedExecutionException:Task [email protected] rejected from [email protected]814013[running, pool size = 8, Active threads = 8, queued tasks = 5, completed tasks = 0] at java.util.concurrent.threadpoolexecutor$abortpolicy.rejectedexecution (Unknown Source) at Java.util.concurr Ent. Threadpoolexecutor.reject (Unknown source) at Java.util.concurrent.ThreadPoolExecutor.execute (Unknown source) at COM. Uestc.xst.Main.main (Main.java:73) thread pool number of threads:8, number of tasks waiting to be executed in queue: 5, performed the number of other tasks played: 0Pool-1-thread-3count:0Pool-1-thread-1count:0Pool-1-thread-2count:0Pool-1-thread-4count:0Pool-1-thread-5count:0Pool-1-thread-7count:0Pool-1-thread-6count:0Pool-1-thread-8count:0Pool-1-thread-3count:0Pool-1-thread-2count:0Pool-1-thread-5count:0Pool-1-thread-4count:0Pool-1-thread-1count:0

To continue modifying parameters:

Threadpoolexecutor executor = new Threadpoolexecutor ($, timeunit.milliseconds,new arrayblockingqueue< Runnable> (5));

Output Result:

Thread pool number of threads: 0, number of tasks waiting to be executed in queue: 0, performed the number of other tasks played: 0thread pool Number of threads:1, number of tasks waiting to be executed in queue: 0, performed the number of other tasks played: 0thread pool Number of threads:2, number of tasks waiting to be executed in queue: 0, performed the number of other tasks played: 0thread pool Number of threads:3, number of tasks waiting to be executed in queue: 0, performed the number of other tasks played: 0thread pool Number of threads:4, number of tasks waiting to be executed in queue: 0, performed the number of other tasks played: 0thread pool Number of threads:5, number of tasks waiting to be executed in queue: 0, performed the number of other tasks played: 0thread pool Number of threads:6, number of tasks waiting to be executed in queue: 0, performed the number of other tasks played: 0thread pool Number of threads:7, number of tasks waiting to be executed in queue: 0, performed the number of other tasks played: 0thread pool Number of threads:8, number of tasks waiting to be executed in queue: 0, performed the number of other tasks played: 0thread pool Number of threads:9, number of tasks waiting to be executed in queue: 0, performed the number of other tasks played: 0thread pool Number of threads:10, number of tasks waiting to be executed in queue: 0, performed the number of other tasks played: 0thread pool Number of threads:11, number of tasks waiting to be executed in queue: 0, performed the number of other tasks played: 0thread pool Number of threads:12, number of tasks waiting to be executed in queue: 0, performed the number of other tasks played: 0thread pool Number of threads:13, number of tasks waiting to be executed in queue: 0, performed the number of other tasks played: 0thread pool Number of threads:14, number of tasks waiting to be executed in queue: 0, performed the number of other tasks played: 0Pool-1-thread-1count:0Pool-1-thread-5count:0Pool-1-thread-3count:0Pool-1-thread-2count:0Pool-1-thread-4count:0Pool-1-thread-9count:0Pool-1-thread-7count:0Pool-1-thread-10count:0Pool-1-thread-8count:0Pool-1-thread-6count:0Pool-1-thread-11count:0Pool-1-thread-13count:0Pool-1-thread-14count:0Pool-1-thread-15count:0Pool-1-thread-12count:0

Results Analysis:

When thread pool threads reach the core thread size, if it is still on, it is prioritized in the task cache queue, and if the queue is full, a new thread is opened if the maximunpoolsize is not full, and an exception is thrown if the maximunpoolsize is full.

Java thread pool

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.