a few common thread pools5.1
Newcachedthreadpool
Creates a cacheable thread pool that can flexibly reclaim idle threads if the thread pool length exceeds the processing needs, and creates a new thread if it is not recyclable.
This type of thread pool is characterized by:
- There is almost no limit to the number of worker threads created ( there are actually limits , and the number is Interger. Max_value), which gives you the flexibility to add threads to the thread pool.
- If a task is not submitted to the thread pool for a long time, that worker thread will automatically terminate if the worker thread is idle for the specified time ( by default , 1 minutes ). After termination, if you submit a new task again, the thread pool re-creates a worker thread.
- When using Cachedthreadpool , be sure to pay attention to the number of control tasks, otherwise, because a large number of threads running at the same time, it will cause system paralysis.
The sample code is as follows:
1Package test;2Import Java.util.concurrent.ExecutorService;3Import java.util.concurrent.Executors;4PublicClassthreadpoolexecutortest {5PublicStaticvoidMain (string[] args) {6 Executorservice Cachedthreadpool =Executors.newcachedthreadpool ();7for (int i =0; I <10; i++) {8 finalint index =I9Try{Ten Thread.Sleep (Index *1000);11}Catch(Interruptedexception e) {12 E.printstacktrace (); }14 Cachedthreadpool.execute ( new Runnable () {15 public void run () {16 System. out.println (index) }18 }); 19 }20 }< Span style= "COLOR: #008080" >21}
5.2
Newfixedthreadpool
Creates a thread pool that specifies the number of worker threads. Each time a task is committed, a worker thread is created, and if the number of worker threads reaches the initial maximum number of thread pools, the submitted task is credited to the pool queue.
Fixedthreadpool is a typical and excellent thread pool that has the advantage of thread pooling to increase program efficiency and save the overhead of creating threads. However, when the online pool is idle, that is, the thread pool does not have a running task, it does not release the worker thread and consumes some system resources.
The sample code is as follows:
Package Test;import Java.util.concurrent.executorservice;import java.util.concurrent.Executors;PublicClassthreadpoolexecutortest {PublicStaticvoidMain (string[] args) {Executorservice Fixedthreadpool = Executors.newfixedthreadpool (3for (int i = 0; i < 10; I++int index = I; Fixedthreadpool.execute (new Runnable () {public void run () {try {system. Out.println (index); Thread.Sleep (2000catch (interruptedexception e) {e.printstacktrace ();}}); } }}
Because the thread pool size is 3, each task outputs index after sleep for 2 seconds, so 3 digits are printed every two seconds .
The size of a fixed-length pool is best set based on system resources such as Runtime.getruntime (). Availableprocessors ().
5.3
Newsinglethreadexecutor
Create a single-threaded executor that onlycreates a unique worker thread to perform the task, which executes the task with only a single worker thread, ensuring that all tasks are executed in the specified order (FIFO, LIFO, priority ) . If this thread ends abnormally, there will be another one to replace it, guaranteeing sequential execution. The most important feature of a single worker thread is that it is guaranteed to perform each task sequentially and that no multiple threads are active at any given time.
The sample code is as follows:
Package Test;import Java.util.concurrent.executorservice;import java.util.concurrent.Executors;PublicClassthreadpoolexecutortest {PublicStaticvoidMain (string[] args) {Executorservice Singlethreadexecutor =Executors.newsinglethreadexecutor ();for (int i = 0; i < 10; I++int index = I; Singlethreadexecutor.execute (new Runnable () {public void run () {try {system. Out.println (index); Thread.Sleep (2000catch (interruptedexception e) {e.printstacktrace ();}}); } }}
5.4
Newschedulethreadpool
Creates a fixed-length thread pool, and supports timed and recurring task executions, supporting timed and recurring task executions.
Deferred 3 seconds execution, deferred execution sample code is as follows:
Package Test;import Java.util.concurrent.executors;import Java.util.concurrent.scheduledexecutorservice;import Java.util.concurrent.TimeUnit;PublicClass threadpoolexecutortest {public static void main (string[] args) {Scheduledexecutorservice Scheduledthreadpool = Executors.newscheduledthreadpool ( 5new Runnable () {public Span style= "COLOR: #0000ff" >void run () {System. Out.println ( "delay 3 seconds ");}, 3, timeunit.seconds); }}
Represents a delay of 1 seconds and executes every 3 seconds, and the sample code is executed periodically as follows:
Package Test;import Java.util.concurrent.executors;import Java.util.concurrent.scheduledexecutorservice;import Java.util.concurrent.TimeUnit;PublicClassthreadpoolexecutortest {public static void main (string[] args) {Scheduledexecutorservice Scheduledthreadpool = Executors.newscheduledthreadpool (5new Runnable ( {public void run () { System. out.println ( "delay 1 Seconds, and Excute every 3 seconds ");}, 1, 3
Several thread pools commonly used in Java