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)