Java thread Cheng Fixedthreadpool (Java Code Combat-003)

Source: Internet
Author: User

ImportJava.util.concurrent.ExecutorService;Importjava.util.concurrent.Executors;ImportJava.util.concurrent.ThreadPoolExecutor;/*** Fixthreadpoolexecutortest defines a reusable thread pool for the number of solid threads*/ Public classFixthreadpoolexecutortest { Public Static voidMain (string[] args) {/** Set the number of threads that handle the request task to a fixed ten, corepoolsize = Maxpoolsize = ten * KeepAliveTime set to 0L, which means the idle thread terminates immediately * wo Rkqueue uses linkedblockingqueue, but does not have a range, then the maximum value (integer.max_value) * This is basically equivalent to an unbounded queue. What are the implications of using this unbounded queue? When the number of threads in the thread pool equals corepoolsize *, the task is added to the blocking queue Workqueue if the task continues to commit, and when the blocking queue is full, the thread pool creates new threads to perform tasks until maximumpoolsize. * Because Fixedthreadpool uses the "unbounded queue" linkedblockingqueue, the maximumpoolsize parameter is invalid, and the Deny policy AbortPolicy specified also will not be valid.         Also, the thread pool does not reject the submitted task, and if the client submits the task faster than the execution of the task, then KeepAliveTime is also an invalid parameter. */Executorservice Pool= Executors.newfixedthreadpool (10); //There is always only 10 threads to handle the request task, and the task will be placed in the blocking queue for too late processing         for(inti = 0; I < 50; i++) {Pool.submit (NewRunnable () {@Override Public voidrun () {System.out.println ("Hello world! Execute threadname= "+Thread.CurrentThread (). GetName ());        }            }); }    }}

Java thread Cheng Fixedthreadpool (Java Code Combat-003)

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.