Using the Java native thread pool class

Directly on the usage of code:

 1 import java.util.concurrent.LinkedBlockingQueue;
 2 import java.util.concurrent.ThreadPoolExecutor;
 3 import java.util.concurrent.TimeUnit;
 4 
 5 public class Fruit {
 6 
 7     public static void main(String[] args) throws InterruptedException{
 8         ThreadPoolExecutor threadPool = new ThreadPoolExecutor(10, 15, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
 9         threadPool.execute(new Runnable(){
10             public void run(){
11                 //do something
12             }
13         });
14     }
15 }

ThreadPool a new look at the parameters used:

1. The first argument 10 means that the thread pool initialization 10 threads work on the inside

2. The second argument 15 means that if 10 threads is not enough, automatically increased to a maximum of 15 threads

3. The third parameter and fourth parameters TimeUnit.SECONDS 60 represents 60 seconds elapse, the extra threads have not received a mandate to dry, then recovered, keeping the thread pool is 10 threads

4. The fifth parameter new LinkedBlockingQueue <Runnable> () is used to put the set task

The execute method is used to add a new task

Written on Sept. 30th, 2019

Guess you like

Origin www.cnblogs.com/LittleMike/p/11613585.html