In performance optimization, one of them is the use of the thread pool. A preliminary discussion of the thread pool, from a brief understanding of the source code analysis, in-depth understanding can be used properly.
(1) Sources and advantages of thread pools
A single asynchronous task needs to create and destroy threads each time. When the task is processed for a short time but the number of requests is huge, excessive resource consumption will occur.
For example, database connection consumes a lot of resources, and the establishment of a thread pool can reuse the database access thread.
Advantages of using a thread pool:
1. reduce resource consumption. Reuse existing threads to reduce thread creation and destruction overhead.
2. Improve the response speed. Task startup, without the need for thread creation, and the speed is improved.
3. Strong thread control. Such as scheduled, scheduled, and single-thread settings.
(2) thread pool category
Four thread pool types:
1. newCachedThreadPool
Cache thread pool, suitable for a large number of short-term tasks
2. newFixedThreadPool
A fixed thread pool that can control the maximum number of concurrent threads
3. newScheduledThreadPool
Fixed-length thread pool, which can be scheduled and periodically executed
4. newSingleThreadExecutor
Single-threaded thread pool to ensure that all tasks are executed in order
(3) Analysis of thread pool category instances
1. newCachedThreadPool
private void newCacheThreadPool() { ExecutorService threadPool = Executors.newCachedThreadPool(); for (int i = 0; i < 5; i++) { final int index = i; try { if (i == 1 || i == 3) { Thread.sleep(100); } } catch (InterruptedException e) { e.printStackTrace(); } threadPool.execute(new Runnable() { @Override public void run() { System.out.println("new cached thread pool " + index + ":" + Thread.currentThread().getId()); } }); } }Running result:
new cached thread pool 0:9new cached thread pool 1:9new cached thread pool 2:10new cached thread pool 3:10new cached thread pool 4:9
Example Analysis: Because thread 1 and thread 3 have a break and wait for other threads to finish execution, the old thread is reused.
2. newFixedThreadPool
private void newFixedThreadPool() { ExecutorService threadPool = Executors.newFixedThreadPool(2); for (int i = 0; i < 5; i++) { final int index = i; threadPool.execute(new Runnable() { @Override public void run() { System.out.println("new fixed thread pool " + index + ":" + Thread.currentThread().getId()); } }); } }Running result:
new fixed thread pool 0:9new fixed thread pool 1:10new fixed thread pool 2:9new fixed thread pool 4:9new fixed thread pool 3:10
Example Analysis: The length is set to 2. Other threads enter the queue and run only after the execution of the first two threads is complete.
3. newScheduledThreadPool
private void newScheduledThreadPool() { ScheduledExecutorService threadPool = Executors.newScheduledThreadPool(2); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } threadPool.scheduleAtFixedRate(new Runnable() { @Override public void run() { System.out.println("new scheduled thread pool :" + Thread.currentThread().getId()); } }, 1, 3, TimeUnit.SECONDS); }Running result:
new scheduled thread pool :9new scheduled thread pool :9new scheduled thread pool :9new scheduled thread pool :9new scheduled thread pool :9new scheduled thread pool :9new scheduled thread pool :9new scheduled thread pool :9
Example: The task starts with a delay of 1 second and runs every 3 seconds.
4. newSingleThreadExecutor
private void newSingleThreadPool() { ExecutorService threadPool = Executors.newSingleThreadExecutor(); for (int i = 0; i < 5; i++) { final int index = i; threadPool.execute(new Runnable() { @Override public void run() { System.out.println("new single thread pool " + index + ":" + Thread.currentThread().getId()); } }); } }Running result:
new single thread pool 0:9new single thread pool 1:9new single thread pool 2:9new single thread pool 3:9new single thread pool 4:9
Example Analysis: there is only one thread, so only one thread can be executed at a time, and other tasks are waiting.
For details about the thread pool source code analysis, refer to java-Thread Pool (2)