To learn and use the thread pool

Thread Pool

ThreadPoolExecutor类

Reference: https://www.cnblogs.com/dolphin0520/p/3932921.html

Construction method:

ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue  workQueue)

ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue  workQueue, RejectedExecutionHandler handler)

ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue  workQueue, ThreadFactory threadFactory)

ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue  workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler)

Methods of parameters:

corePoolSize: the size of the core pool

  1. By default, the thread pool and no thread, the thread pool is created, the number of threads in the thread pool is zero. Before the arrival of a mission to create a thread to perform the task, unless you call the prestartAllCoreThreads () or prestartCoreThread () method,
  2. When the number of threads in the thread pool reaches corePoolSize, the task will reach into the cache queue among

maximumPoolSize: The maximum number of threads in the thread pool represents the maximum number of threads in the thread pool can create

keepAliveTime: how long will represent up to maintain the thread does not terminate when the task execution

  1. Only when the number of threads in the thread pool greater than corePoolSize, keepAliveTime will work
  2. If a thread idle time to reach keepAliveTime, will be terminated until the number of threads in the thread pool does not exceed corePoolSize
  3. If you call allowCoreThreadTimeOut (boolean) method, when the number of threads in the thread pool is not more than corePoolSize, keepAliveTime parameters come into play until the number of threads in the pool 0

unit: keepAliveTime parameters unit of time, there are seven kinds of values

TimeUnit.DAYS;               //天
TimeUnit.HOURS;             //小时
TimeUnit.MINUTES;           //分钟
TimeUnit.SECONDS;           //秒
TimeUnit.MILLISECONDS;      //毫秒
TimeUnit.MICROSECONDS;      //微妙
TimeUnit.NANOSECONDS;       //纳秒

workQueue: blocking queue is used to store task waiting to be executed

Choose to block queue

ArrayBlockingQueue;
LinkedBlockingQueue;
SynchronousQueue; 

Queuing policy related to the thread pool and BlockingQueue

threadFactory: a thread factory used to create the main thread

handler: indicate when the policy of denial when processing tasks, the following four values:

ThreadPoolExecutor.AbortPolicy:丢弃任务并抛出RejectedExecutionException异常。 
ThreadPoolExecutor.DiscardPolicy:也是丢弃任务,但是不抛出异常。 
ThreadPoolExecutor.DiscardOldestPolicy:丢弃队列最前面的任务,然后重新尝试执行任务(重复此过程)
ThreadPoolExecutor.CallerRunsPolicy:由调用线程处理该任务 

Common methods:

execute (): submit a job to the thread pool, thread pool to execute handed over

submit (): submit jobs to the thread pool, and returns the result of task execution, the underlying call to execute () method, except that it utilizes the Future to get the task execution results

shutdown () close the thread pool

shutdownNow () close the thread pool

Member variables:

private final BlockingQueue<Runnable> workQueue;              //任务缓存队列,用来存放等待执行的任务
private final ReentrantLock mainLock = new ReentrantLock();   //线程池的主要状态锁,对线程池状态(比如线程池大小
                                                              //、runState等)的改变都要使用这个锁
private final HashSet<Worker> workers = new HashSet<Worker>();  //用来存放工作集
 
private volatile long  keepAliveTime;    //线程存货时间   
private volatile boolean allowCoreThreadTimeOut;   //是否允许为核心线程设置存活时间
private volatile int   corePoolSize;     //核心池的大小(即线程池中的线程数目大于这个参数时,提交的任务会被放进任务缓存队列)
private volatile int   maximumPoolSize;   //线程池最大能容忍的线程数
 
private volatile int   poolSize;       //线程池中当前的线程数
 
private volatile RejectedExecutionHandler handler; //任务拒绝策略
 
private volatile ThreadFactory threadFactory;   //线程工厂,用来创建线程
 
private int largestPoolSize;   //用来记录线程池中曾经出现过的最大线程数
 
private long completedTaskCount;   //用来记录已经执行完毕的任务个数

volitile keyword

Reference: https://www.cnblogs.com/zhengbin/p/5654805.html#_label0

volitile features: This variable visibility to ensure all of the threads

The visibility of the program: to ensure that the read operation thread, timely see the value of the other threads written, in order to ensure visibility among multiple threads of memory write operations, you must use the synchronization mechanism.

Visibility means visibility between threads, one thread is visible to the modified state of another thread

That is the result of a modification of the thread. Another thread can immediately see. For example: using volatile variables will have visibility

In Java volatile, synchronized and the final realization of visibility.

Thread pool status

volatile int runState;

static final int RUNNING = 0;

static final int SHUTDOWN = 1;

static final int STOP = 2;

static final int TERMINATED = 3;

runState: Indicates the current thread pool

volatile variables: to ensure visibility between threads

RUNNING: create a state of thread pool

SHUTDOWN: call the shutdown () method,

                       此时线程池不能够接受新的任务,它会等待所有任务执行完毕 

STOP: call shutdownNow () method

     此时线程池不能接受新的任务,并且会去尝试终止正在执行的任务 

TERMINATED: when the thread pool is in STOP or SHUTDOWN state, and all the worker threads have been destroyed, the task queue is empty cache or execution ends

Ha ha ha

Guess you like

Origin www.cnblogs.com/Auge/p/11609909.html