preface

I recently changed my job. Found old age. I just don’t have a good memory. I can’t remember a lot of details. As the saying goes, a bad memory is better than a bad memory. Ready to clean up the thread pool family. On the one hand, it is convenient to browse. On the one hand, we can make progress together.

Of inside roll fierce should learn or learn!

series

  • Thread Pool Series – (1) Background
  • Thread pool series – (2) Thread pool status
  • Thread pool series – (3) Rejection policy
  • Thread Pool series – (4) Workflow
  • Thread pool series – (5) Shutdown && shutdownNow
  • Thread pool – (6) Submit

Background to thread pool generation

The appearance of a thing is bound to solve a certain problem. Thread pools are no exception. Think of some. If thread pools are not used. Go to New Thread() when you need to do asynchronous operations in Android development. When a lot of tasks need to be created, there can be a lot of resource overhead. And multiple threads. Its management is also very cumbersome.

The emergence of thread pool can effectively deal with a series of problems caused by multithreading.

The implementation principle of Java thread pool and its practice in meituan business are very recommended. It is a more detailed article that I have read about thread pool.

Advantages of thread pools

Thread pool is just using the idea of pooling, will have no constraint management threads to achieve unified management, still reference Java thread pool implementation principle and several advantages summarized in the practice of Meituan business.

  • Reduced resource consumption: Reuse of created threads through pooling techniques to reduce wastage from thread creation and destruction.

  • Improved response time: Tasks can be executed immediately when they arrive without waiting for threads to be created.

  • Improve manageability of threads: Threads are scarce resources. If they are created without limit, they will not only consume system resources, but also cause resource scheduling imbalance due to unreasonable distribution of threads, which reduces system stability. Thread pools allow for uniform allocation, tuning, and monitoring.

  • More and more power: Thread pools are extensible, allowing developers to add more functionality to them. Such as delay timer thread pool ScheduledThreadPoolExecutor, allows a stay of execution or regular task execution.

Creating a thread pool

Create a thread pool by following the Java Executors class to quickly create a thread pool.

Inside each is a ThreadPoolExecutor

public ThreadPoolExecutor(int corePoolSize,
                          int maximumPoolSize,
                          long keepAliveTime,
                          TimeUnit unit,
                          BlockingQueue<Runnable> workQueue,
                          ThreadFactory threadFactory,
                          RejectedExecutionHandler handler) {
Copy the code

We can customize a thread pool to suit our needs. In okHTTP, for example, a thread pool is specified

  • Okhttp 3.14.9 Javaversion
public synchronized ExecutorService executorService(a) {
  if (executorService == null) {
    executorService = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60, TimeUnit.SECONDS,
        new SynchronousQueue<>(), Util.threadFactory("OkHttp Dispatcher".false));
  }
  return executorService;
}
Copy the code
  • Okhttp 4.9.0 kotlin version
@get:Synchronized
@get:JvmName("executorService") val executorService: ExecutorService
  get(a) {
    if (executorServiceOrNull == null) {
      executorServiceOrNull = ThreadPoolExecutor(0, Int.MAX_VALUE, 60, TimeUnit.SECONDS,
          SynchronousQueue(), threadFactory("$okHttpName Dispatcher".false))}return executorServiceOrNull!!
  }
Copy the code

Parameter is introduced

CorePoolSize Number of core threads

MaximumPoolSize specifies the maximum number of threads.

KeepAliveTime and unit determine a time together. Represents how long the thread has not executed a task. The thread will be reclaimed.

A workQueue is a blocking queue of threaded tasks. More on this later.

The threadFactory thread creates the factory

Handler Thread pool rejection policy

The details can be seen in this series