Executors thread pool plant (ThreadPoolExecutor)
newSingleThreadPool ();
single thread pool to ensure that the task execution order. It encapsulates the task queue thread pool and thread pool lifecycle. Underlying the use of LinkedBlockingQueue, maximum Integer.MAX_VALUE, Ali Developer's Guide does not recommend using a thread pool jdk offered no denial strategy.
() newCachedThreadPool;
core thread count is 0, the maximum number of threads is Integer.MAX_VALUE, the task queue is SynchronousQueue.
We have a mission to create a new thread
newFixedThreadPool ();
fixed length threads in the thread pool, pass through the same core threads and the parameter value of the maximum number of threads. But LinkedBlockingQueue also used.
(In the case to ensure that the task is not stacked, the number of tasks fluctuated use Cached, relatively stable number of tasks using the Fixed, Fixed are executed in parallel, high efficiency)
newScheduledThreadPool ();
scheduled task. Thread maximum of Integer.MAX_VALUE, using delay Delay queue.
(Time frame: Quartz, cron)
ForkJoinPool
newWorkStealingPool (); (bottom packages ForkJoinPool)
Each thread has its own separate queue. If the current queue is empty, go back to the queue to steal other threads into their own queue.
Contrast ThreadPoolExecutor, is the thread to pick up a task queue task.
And ForkJoinPool Each thread maintains its own task queue when the thread itself the task queue is empty inside, will be through work stealing algorithm (lock), to the other threads take a task queue to execute. Were split tasks (Fork process)
ForkJoinPool'll After performing, (Join process) and finally rolled up to the parent task, the final summary to the root task
//ForkJoinPool执行需要继承
static class AddTask extends RecursiveAction//无返回值
static class AddTaskRet extends RecursiveTask<Long>//有返回值
ParalleStreamAPI
Parallel processing api, the bottom layer is used ForkJoinPool
List<Integer> nums = new ArrayList<>();
Random r = new Random();
for(int i=0; i<10000; i++) nums.add(1000000 + r.nextInt(1000000));
nums.parallelStream().forEach(ParallelStreamAPI::doSomeThing);
Thread pool size
The utilization ratio of the processor and the size of the thread pool may be estimated using the following equation:
N = N Threads the U-CPU CPU * * (W is +. 1 / C)
NCPU is the number of processor cores can be produced by
Runtime.getRuntime().availableProcessors()
Ucup CPU utilization is desired (between 0-1)
W is / C ratio of waiting time (wait, computation) computation time [thread how long to let the CPU, multiple CPU prolonged use]
Concurrent、Parallel
Concurrency: Task submitted
in parallel: task execution
parallelism is a subset of concurrent
Thread pool status
RUNNING: the implementation of
SHUTDOWN: Stop
STOP: to stop immediately
TIDYING: SHUTDOWN completed, is finishing
TERMINATE: thread completely finished
Thread Pool execute () execution order
Create kernel threads, kernel threads full load to the queue, the queue full application threads try again, and if that fails, refused to execute strategy.
addWorker (), the number of threads +1, real addWorker (), the last start ()