Java-17.7 Executor)
In this section, we will discuss the Executor thread pool.
1. What is a thread pool? (The following explanation comes from the http://baike.haosou.com/doc/2511076-2653416.html)
A thread pool is a form of multi-threaded processing. during processing, tasks are added to the queue, and these tasks are automatically started after the thread is created. All threads in the thread pool are backend threads. Each thread uses the default stack size, runs at the default priority, and is in a multi-threaded unit. If a thread is idle in the managed code (such as waiting for an event), the thread pool inserts another auxiliary thread to keep all processors busy. If all threads in the thread pool remain busy, but the queue contains pending jobs, the thread pool will create another auxiliary thread after a period of time, but the number of threads will never exceed the maximum value. Threads that exceed the maximum value can be queued, but they will not start until other threads are completed.
2. role:
(1) centralized thread management
(2) Improve the thread utilization, because the application starts multiple threads, which may be sleep or waiting for most of the time, we need to use them.
3. Example:
(1) Number of threads automatically managed by the System
package com.ray.ch17;import java.util.Random;import java.util.concurrent.Callable;import java.util.concurrent.ExecutionException;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.Future;public class Test {public static void main(String[] args) throws InterruptedException,ExecutionException {ExecutorService executors = Executors.newCachedThreadPool();Callable
callable = new MyCall();Future
future = executors.submit(callable);System.out.println(future.get());executors.shutdown();}}class MyCall implements Callable
{@Overridepublic Integer call() throws Exception {System.out.println("doing something...");Thread.sleep(1000);return new Random().nextInt(50);}}
Output:
Doing something...
17
(2) manually set the number of threads
package com.ray.ch17;import java.util.Random;import java.util.concurrent.Callable;import java.util.concurrent.ExecutionException;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.Future;public class Test {public static void main(String[] args) throws InterruptedException,ExecutionException {ExecutorService executors = Executors.newFixedThreadPool(2);Callable
callable = new MyCall();Future
future = executors.submit(callable);System.out.println(future.get());executors.shutdown();}}class MyCall implements Callable
{@Overridepublic Integer call() throws Exception {System.out.println("doing something...");Thread.sleep(1000);return new Random().nextInt(50);}}
Output:
Doing something...
17
4. Notes:
(1) When creating a thread pool, we can determine the number of threads based on the actual situation or by the system.
(2) Use the submit method to submit Runnable or Callable tasks
(3) If you want to cancel a task or submit a Callable task, you must save the objects returned by Future.
(4) Add the shundown method to close the thread pool.
Summary: This section describes the Executor thread pool.