preface

This week I sent out my resume as a Java backend development engineer. Meituan’s interviewer interviewed me this week. During the interview he asked about thread pools, and today I’ll talk more about Java thread pools.

The thread pool

A thread pool maintains multiple threads, waiting for the supervisor to assign tasks that can be executed concurrently. This avoids the cost of creating and destroying threads while working on short-duration tasks.

Start () creates a number of thread pools to loop through. Stop () stops all thread loops and reclaims all resources. AddTask () adds a taskCopy the code

Excutors creates a thread pool as follows:

Executors.newFixedThreadPool(100);// Create a thread pool of fixed size
Executors.newSingleThreadExecutor();// Create a pool with only one thread
Executors.newCachedThreadPool();// Create a thread pool with an unlimited number of threads, and any submitted tasks will be executed immediately
Copy the code

For programs that need to run long on the server, the constructor of ThreadPoolExecutor should be used to create thread pools

public ThreadPoolExecutor(
      intCorePoolPoolSize,// The number of threads that the thread pool can sustain for a long timeintMaximumPoolSize, // Maximum number of threadslongKeepAliveTime,// Free thread lifetime TimeUnit unit,// BlockingQueue<Runnable> workQueue,// Task queue ThreadFactory ThreadFactory,// New thread generation method RejectedExecutionHandler handler
Copy the code

Java thread pools have seven parameters and four features.

Feature 1: When the number of running threads (including idle threads) in the pool is smaller than corePoolSize, a new thread executes the task.

Feature 2: When the number of running threads in the pool is greater than or equal to corePoolSize, the newly inserted task enters the workQueue (if the workQueue length allows) and waits for the idle thread to execute it.

Feature 3: When the number of tasks in the queue reaches the upper limit and the number of running threads in the pool is smaller than maximumPoolSize, a new thread is created for the newly added task.

Feature 4: When the number of tasks in the queue reaches the upper limit and the number of running threads in the pool is equal to maximumPoolSize, a reject policy is implemented for new tasks (the default reject policy of the thread pool is throwing exceptions).

species

newCachedThreadPool

  • The number of core threads is 0, and the maximum number of threads isInteger.MAX_VALUE
public static ExecutorService newCachedThreadPool(a) {
        return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                      60L, TimeUnit.SECONDS,
                                      new SynchronousQueue<Runnable>());
    }
Copy the code

role

Create a thread pool that can create new threads as needed, but reuse previously constructed threads as they become available and create new threads using the supplied ThreadFactory as needed.

Characteristics of the

(1) The number of threads in the thread pool is not fixed and can reach the maximum value (interger.max_value). (2) Threads in the thread pool can be reused and reclaimed by cache (the reclaimed time is 1 minute by default). (3) When there are no available threads in the thread pool, a new thread will be created

Create a way

Executors.newCachedThreadPool();

newFixedThreadPool

  • Both the number of core threads and the maximum number of threads are specified nThreads

  • The lifetime of idle threads is 0

  • A work queue is an unbounded queue

public static ExecutorService newFixedThreadPool(int nThreads) {
        return new ThreadPoolExecutor(nThreads, nThreads,
                                      0L, TimeUnit.MILLISECONDS,
                                      new LinkedBlockingQueue<Runnable>());
    }
Copy the code

role

Create a reusable thread pool with a fixed number of threads to run in a shared unbounded queue. At any given point, most nThreads threads will be active for processing tasks. If the attached task is submitted while all threads are active, the attached task will wait in the queue until there are available threads. If any thread terminates due to failure during execution prior to closure, a new thread will replace it to perform subsequent tasks (if needed). Threads in the pool will remain in existence until a thread is explicitly closed.

Characteristics of the

(1) the number of threads in the thread pool is a certain amount, which can well control the number of concurrent threads. (2) Threads can be used repeatedly, and will exist until the display is closed. (3) When more than a certain number of threads are submitted, they need to wait in the queue

Create a way

(1) Executors. NewFixedThreadPool (intNThreads);//nThreads indicates the number of threads2) Executors. NewFixedThreadPool (intNThreads, ThreadFactory ThreadFactory);//nThreads specifies the number of threads. ThreadFactory specifies the factory method for creating threads
Copy the code

newSingleThreadExecutor

  • The number of core threads and the maximum number of threads are both 1
  • A work queue is an unbounded queue
public static ExecutorService newSingleThreadExecutor(a) {
        return new FinalizableDelegatedExecutorService
            (new ThreadPoolExecutor(1.1.0L, TimeUnit.MILLISECONDS,
                                    new LinkedBlockingQueue<Runnable>()));
    }
Copy the code

role

Create an Executor that uses a single worker thread and runs it as an unbounded queue. (Note that if this single thread is terminated due to a failure during execution prior to closure, a new thread will replace it to perform subsequent tasks if needed). Each task is guaranteed to be executed sequentially and that no more than one thread is active at any given time. Unlike the equivalent newFixedThreadPool(1), you are guaranteed to use another thread without reconfiguring the executable returned by this method.

Characteristics of the

A maximum of 1 thread is executed in the thread pool, after which the submitted thread activity is queued for execution.

Create a way

(1) Executors. NewSingleThreadExecutor (); (2) Executors. NewSingleThreadExecutor (ThreadFactory ThreadFactory);// threadFactory Specifies the factory method for creating threads
Copy the code

newScheduledThreadPool

  • Specifies the number of core threads corePoolSize
  • The maximum number of threads is integer.max_value
  • DelayedWorkQueue: The task queue is executed based on the priority of the task delay time
public class ScheduledThreadPoolExecutor
        extends ThreadPoolExecutor
        implements ScheduledExecutorService {.../**
     * Creates a new {@code ScheduledThreadPoolExecutor} with the
     * given core pool size.
     *
     * @param corePoolSize the number of threads to keep in the pool, even
     *        if they are idle, unless {@code allowCoreThreadTimeOut} is set
     * @throws IllegalArgumentException if {@code corePoolSize < 0}
     */
    public ScheduledThreadPoolExecutor(int corePoolSize) {
        super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
              newDelayedWorkQueue()); }... }Copy the code

role

Create a thread pool that can schedule commands to run after a given delay or to execute them periodically.

Characteristics of the

(1) there are a specified number of threads in the thread pool, and even empty threads are retained. (2) Thread activities can be timed or deferred

Create a way

(1) Executors. NewScheduledThreadPool (intCorePoolSize);// corePoolSize Specifies the number of threads2NewScheduledThreadPool ()intCorePoolSize the ThreadFactory ThreadFactory);// corePoolSize Specifies the number of threads. ThreadFactory Specifies the factory for creating threads
Copy the code

newSingleThreadScheduledExecutor

role

Create a single-threaded executor that can schedule commands to run after a given delay or to execute them periodically.

Characteristics of the

(1) a maximum of one thread is executed in the thread pool, after which the submitted thread activity will be queued for execution. (2) Thread activity can be timed or deferred

Create a way

(1) Executors. NewSingleThreadScheduledExecutor (); (2) Executors. NewSingleThreadScheduledExecutor (ThreadFactory ThreadFactory);//threadFactory Specifies the factory for creating threads
Copy the code

The work queue

SynchronousQueue: Commit directly

Is the default option for work queues to submit tasks directly to the thread without holding them.

If there is no thread available to run the task immediately, an attempt to queue the task will fail, so a new thread will be constructed.

The advantages and disadvantages

Advantages: Locks can be avoided when processing request sets that may have internal dependencies.

Direct submission usually requires unbounded maximumPoolSizes to avoid rejecting newly submitted tasks.

This policy allows the possibility of unbounded threads growing when commands arrive consecutively at an average that exceeds what the queue can handle.

ArrayBlockingQueue: a bounded queue

When used with limited Sizes maximumPoolSizes, bounded queues help prevent resource exhaustion, but can be more difficult to adjust and control.

Queue size and maximum pool size can be tradeoffs

  • Using large queues and small pools minimizes CPU utilization, operating system resources, and context switch overhead, but can result in artificially low throughput.

  • If tasks block frequently (for example, if they are I/ O-bound), the system may be able to schedule more threads that are not scheduled.

  • Using small queues typically requires a larger pool size, which keeps the CPU busy but can lead to unacceptable scheduling overhead, which also reduces throughput.

LinkedBlockingQueue: An unbounded queue

New tasks wait in the queue while all core threads are busy. Therefore, just create the corePoolSize thread. (The value of maximumPoolSize has no effect.) When each task is completely independent of the others, tasks do not affect each other’s execution.

The advantages and disadvantages

Advantages: For example, in a Web server, although this queuing is useful for eliminating short bursts of requests. Disadvantages: The work queue grows unlimitedly when command requests arrive faster than they can be processed.

Rejection policies

AbortPolicy: throws runtime RejectedExecutionException handler was refused

DiscardPolicy: Unexecutable tasks will be deleted

DiscardOldestPolicy: If the executing program is not closed, the task at the head of the work queue will be deleted and the executing program will be retry (repeat the process if it fails again)

CallerRunsPolicy: The thread calls the Execute itself that runs the task. This strategy provides a simple feedback control mechanism that slows down the delivery of new tasks.

RejectedExecutionHandler rejected = null;

rejected = new ThreadPoolExecutor.AbortPolicy();// By default, an exception is thrown when the queue is full

rejected = new ThreadPoolExecutor.DiscardPolicy();// The queue is full of lost tasks

rejected = new ThreadPoolExecutor.DiscardOldestPolicy();// Delete the first queued task and try to join the queue later

rejected = new ThreadPoolExecutor.CallerRunsPolicy();// If adding to the thread pool fails, the main thread will perform the task itself
Copy the code

Reference link: www.cnblogs.com/cdf-opensou… Refer to the link: www.cnblogs.com/vince66/p/9… Reference link: www.mamicode.com/info-detail…

conclusion

Let’s have fun. Let’s have fun. Don’t joke about the interview.

Thread pool memory tips: seven parameters, four features, five categories, three work queues, and four rejection policies

Thread pool, in the interview appears very many times, we should keep in mind the knowledge before the interview.