Home  >  Article  >  Java  >  Briefly summarize the four creation methods of java thread pool

Briefly summarize the four creation methods of java thread pool

WBOY
WBOYforward
2022-07-21 17:55:371742browse

This article brings you relevant knowledge about java. It mainly introduces the detailed analysis of the four creation methods of java thread pool. The connection pool is a technology for creating and managing a connection buffer pool. , these connections are ready to be used by any thread that needs them. Let’s take a look at them. I hope it will be helpful to everyone.

Briefly summarize the four creation methods of java thread pool

Recommended study: "java Video Tutorial"

Foreword

Before talking about the thread pool, I will add it first The definition of connection pool

Connection pool is the technology of creating and managing a buffer pool of connections that are ready to be used by any thread that needs them

You can see the functions of the connection pool as follows:

1. Thread pool

Thread pool (English: thread pool) : A thread usage pattern. Too many threads will bring scheduling overhead, which will affect cache locality and overall performance. The 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 when handling short-lived tasks. The thread pool can not only ensure full utilization of the core, but also prevent excessive scheduling

Features:

  • Reduce resource consumption: Through repetition Utilize created threads to reduce the consumption caused by thread creation and destruction. Improve response speed: When a task arrives, the task can be executed immediately without waiting for thread creation. Improve the manageability of threads : Threads are scarce resources. If they are created without restrictions, they will not only consume system resources, but also reduce the stability of the system. The thread pool can be used for unified allocation and tuning. and monitoring.

Its functions are:

Reuse existing threads, reduce the cost of object creation and destruction, effectively control the maximum number of concurrent threads, and improve the use of system resources. efficiency, while avoiding excessive resource competition and congestion. Provides functions such as scheduled execution, periodic execution, single thread, and concurrency control.

The specific structure is as follows:

2. Creation method

There are 4 creation methods in total They are one thread in a pool, n threads in a pool, scalable threads in a pool, and timing and periodic threads in a pool

Executors.newFixedThreadPool(int)N threads in a pool control the largest Concurrency number, excess threads will wait in the queue

ExecutorService threadPool1 = Executors.newFixedThreadPool(5); //5个窗口

Executors.newSingleThreadExecutor()One thread per pool

 ExecutorService threadPool2 = Executors.newSingleThreadExecutor(); //一个窗口

Executors.newCachedThreadPool()A pool can be expanded to create threads according to needs, and idle threads can be flexibly recycled
After executing the thread, you can continue to use it without expanding it

 ExecutorService threadPool3 = Executors.newCachedThreadPool();

Executors.newScheduledThreadPool(), supports timing and Periodic task execution

//表示延迟一秒,后执行3秒
ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5);
scheduledThreadPool.scheduleAtFixedRate(new Runnable() {
      @Override
      public void run() {
          log.info("delay 1 seconds, and excute every 3 seconds");
 
      }
}, 1, 3, TimeUnit.SECONDS);

Recommended learning: "java video tutorial"

The above is the detailed content of Briefly summarize the four creation methods of java thread pool. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:jb51.net. If there is any infringement, please contact admin@php.cn delete