Home > Java > Javagetting Started > body text

How thread pool is implemented

王林
Release: 2020-02-06 18:26:54
forward
3542 people have browsed it

How thread pool is implemented

The thread pool has the following implementation methods:

Executors currently provides 5 different thread pool creation configurations:

1 , newCachedThreadPool()

It is a thread pool used to handle a large number of short-term work tasks. It has several distinctive features: it will try to cache threads and reuse them. When no cached threads are available, it will Create a new worker thread; if the thread is idle for more than 60 seconds, it will be terminated and the cache will be removed; when idle for a long time, this thread pool will not consume any resources. It uses SynchronousQueue internally as a work queue.

Video tutorial recommendation: java video tutorial

2. newFixedThreadPool (int nThreads)

Reuse the specified number (nThreads) Threads use an unbounded work queue behind them. At most nThreads worker threads are active at any time. This means that if the number of tasks exceeds the number of active threads, it will wait for idle threads to appear in the work queue; if the worker thread exits, a new worker thread will be created to make up for the specified number nThreads.

3. newSingleThreadExecutor()

Its characteristic is that the number of working threads is limited to 1 and it operates an unbounded work queue, so it ensures that all tasks are Being executed sequentially, at most one task will be active, and users are not allowed to change the thread pool instance, so changing the number of threads can be avoided.

4, newSingleThreadScheduledExecutor() and newScheduledThreadPool(int corePoolSize)

creates a ScheduledExecutorService, which can perform scheduled or periodic work scheduling. The difference is that a single worker thread or Multiple worker threads.

5. newWorkStealingPool(int parallelism)

This is a thread pool that is often ignored. This creation method was only added in Java 8, and ForkJoinPool will be built internally. Using the Work-Stealing algorithm, tasks are processed in parallel without guaranteeing the processing order.

Related article tutorial sharing: java quick start

The above is the detailed content of How thread pool is implemented. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template