This is the 24th day of my participation in the August More Text Challenge

preface

Threads, the smallest unit of execution in a program’s execution flow, are the actual operational units in the flow, and are often confused with processes. So what’s the difference between a thread and a process? First, a process is a dynamic process, an active entity. Simply put, the running of an application can be thought of as a process, and threads are the actual task performers in the running. You can say that a process contains multiple threads that can run simultaneously.

ThreadPool

Thread Pool Principle

The so-called thread pool is popularly understood as a pool in which there are already created threads. When a task is submitted to the thread pool for execution, a thread in the pool will actively execute the task. If the number of threads in the pool is insufficient to handle a large number of tasks, new threads need to be automatically added to the pool, but this number is limited, like the water boundary of a pond. When there are fewer tasks, threads in the pool can automatically recycle and release resources. In order to be able to submit tasks asynchronously and cache unprocessed tasks, you need a task queue.

  • Task queue: Used to cache submitted tasks
  • Thread count management: A thread pool must be able to manage and control the number of threads
    • Init: The initial number of threads when the thread pool is created
    • Max: indicates the maximum number of threads when the thread pool is automatically expanded
    • Core: Threads need to be released when the thread pool is idle but also maintain a certain number of active or core threads
    • Init <= core <= Max
  • Task rejection policy: If the number of threads has reached the upper limit and the task queue is full, a corresponding rejection policy is required to notify the task submitter
  • Thread factory: Used to personalize threads, such as making them daemons and setting thread names.
  • QueueSize: The task queue mainly holds the Runnable submitted, but it needs to be limited in order to prevent memory overflow
  • Keepedalive: indicates the time that determines the interval for automatic maintenance of important parameters of a thread.

ThreadPool

  • Execute (Runnable Runnable) : This method accepts the submission of Runnable tasks
  • Shutdown () : Closes and destroys the thread pool
  • GetInittSize () : Returns the initial number of threads in the thread pool
  • GetMaxSize () : Returns the maximum number of threads in the thread pool
  • GetCoreSize () : Returns the number of core threads
  • GetQueueSize () : Returns the number of current thread pool tasks
  • GetActiveCount () : Returns the number of active threads in the thread pool
  • IsShutDown () : Determines whether the thread pool has been destroyed

RunnableQueue

RunnableQueue is mainly used to store the submitted Runnable. This Runnable is a BlockedQueue and has a limit

  • Offer (Runnable Runnable) : The offer method is mainly used to submit tasks to a queue
  • Runnable take () : The method gets the corresponding task from the queue
  • Int size () : This method is used to get the number of runnables in the current queue

ThreadFactory

ThreadFactory provides an interface for creating threads so that you can personalize them, such as which Group they should be placed in, their priority, Thread level, Thread name, and whether they are daemons

@FunctionalInterface
public interface ThreadFactoryThread createThread (Runnable Runnable); }Copy the code

CreateThread (Runnable Runnable) is used to create a thread

There are four common thread pools:

  • CachedThreadPool: a cacheable thread pool in which there are no core threads and the number of non-core threads is integer. max_value, which is an infinite number of threads that are created to perform tasks when needed and recycled when not needed.

  • SecudleThreadPool: a pool of threads that periodically execute tasks on a specific schedule. There are core threads, but there are also non-core threads, which are also infinite in size. This is suitable for performing periodic tasks.

  • SingleThreadPool: Only one thread is used to execute tasks, which is suitable for sequential tasks.

  • FixedThreadPool: a fixed length thread pool with core threads, the maximum number of core threads, and no non-core threads