Executor is the top-level interface of the Java thread pool
One of the most core classes in the interface: the constructor inthreadpoolexecutor :
Public threadpoolexecutor (int corepoolsize,//10
int maximumpoolsize,//10
Long KeepAliveTime,//0L
Timeunit unit,//time unit
Blockingqueue<runnable> workQueue//queue)
First parameter: corePoolSize -the number of threads saved in the pool, including idle threads. indicates how many threads exist when the thread pool is initially initialized.
第二个参数:maximumPoolSize-The maximum number of threads allowed in the pool. the maximum number of threads currently in this thread pool.
第三个参数:keepAliveTime-When the number of threads is greater than the core, this is the maximum time to wait for a new task before terminating the extra idle thread. What is the current thread idle time, for example, 0L, which means that once the thread is executed, it is recycled directly, does not stop, does not make any idle
第四个参数:unit-The time unit of the KeepAliveTime parameter. The third parameter is used with the fourth parameter.
第五个参数:workQueue-the queue used to hold the task before execution. This queue keeps only the Runnable tasks that are submitted by the execute method. If there are no idle threads, the task will be suspended to this queue.
Executors is a class
The Executors class provides a number of static methods for generating different types of thread pools:
1,Executors.newFixedThreadPool(int nThreads) // 创建一个可重用固定线程数的线程池,以共享的无界队列方式来运行这些线程。
If a parameter such as 10 is passed, the 10 threads are initialized, and the maximum number of threads is 10, and the task will be suspended to the unbounded queue if there are no idle threads to hold the task.
2,newCachedThreadPool() //创建一个可根据实际情况调整线程个数的线程池,不限制最大线程数量,如果来了一个任务,就创建一个线程,若无任务则不创建线程,并且每个线程会在60秒后自动回收
The first parameter, Coresize, is 0, which means that the thread is not created when the thread is initialized. There is no thread at the beginning
The second parameter, which indicates how many threads can be loaded, integer.max_value how many threads are not qualified
Third, four parameters, representing 60 seconds, such as a task after the completion of the thread is not immediately recycled, wait 60 seconds, to see if there is a second task, if there is a continuation of execution, if not, it is recycled. So this is called Cachedthreadpool. With the meaning of caching.
Synchronousqueue<runnable> ()
3,
Executors.newscheduledthreadpool (int corepoolsize) creates a thread pool that can schedule commands to run after a given delay or perform periodic
Specific usage: A small example
Importjava.util.concurrent.Executors;ImportJava.util.concurrent.ExecutorService;ImportJava.util.concurrent.ScheduledExecutorService;Importjava.util.concurrent.ScheduledFuture;ImportJava.util.concurrent.TimeUnit;classTempextendsThread { Public voidrun () {System.out.println ("Run"); }} Public classScheduledjob { Public Static voidMain (String args[])throwsException {Temp command=NewTemp (); Scheduledexecutorservice Scheduler= Executors.newscheduledthreadpool (1); //The command represents a task, 5 represents a delay of 5 seconds for initialization, 1 represents every 1 seconds to perform this task, the last is the time unit, this is the secondscheduledfuture<?> scheduleTask = scheduler.schedulewithfixeddelay (Command, 5, 1, Timeunit.seconds); }}
Thread pool Executor and executors