1. Reduces the number of times a thread is created and destroyed, and each worker thread can be reused to perform multiple tasks.
2. You can adjust the number of threads in the thread pool according to the endurance of the system, prevent the server from being exhausted because it consumes too much memory (approximately 1MB of memory per thread, the more threads open, the more memory is consumed and the last crashes).
The top interface of the thread pool in Java is executor, but strictly speaking, executor is not a thread pool, but a tool for executing threads. The real thread pool interface is executorservice.
1. Newsinglethreadexecutor
Creates a single threaded pool of threads. This thread pool has only one thread at work, which is equivalent to single-threaded serial execution of all tasks. If this unique thread ends because of an exception, a new thread will replace it. This thread pool guarantees that the order in which all tasks are executed is performed in the order in which the tasks are submitted.
2.Newfixedthreadpool
Creates a fixed-size thread pool. Each time a task is committed, a thread is created until the thread reaches the maximum size of the threads pool. Once the maximum size of the thread pool is reached, the thread pool will be replenished with a new thread if it ends up executing an exception.
3. Newcachedthreadpool
Creates a cacheable pool of threads. If the size of the thread pool exceeds the thread that is required to process the task,
Then a partially idle (60 second non-performing) thread is reclaimed, and when the number of tasks increases, the thread pool can intelligently add new threads to handle the task. This thread pool does not limit the size of the thread pool, and the thread pool size is entirely dependent on the maximum thread size that the operating system (or JVM) can create.
4.Newscheduledthreadpool
Create a thread pool of unlimited size. This thread pool supports the need to schedule and periodically perform tasks
Here I use 2 examples to illustrate, the thread pool of simple and practical
Import Java.util.concurrent.executorservice;import Java.util.concurrent.executors;import Java.util.concurrent.timeunit;public class ThreadPool {public static void main (string[] args) {//executorservice ThreadPool = Executors.newfixedthreadpool (3); Executorservice ThreadPool = Executors.newcachedthreadpool (); Executorservice ThreadPool = Executors.newsinglethreadexecutor (); for (int i=0;i<=4;i++) {Final int task = i; Threadpool.execute (New Runnable () {@Overridepublic void Run () {for (int j=0;j<=10;j++) {T ry {thread.sleep;} catch (Interruptedexception e) {e.printstacktrace ();} System.out.println (Thread.CurrentThread (). GetName () + "is loopping of" + j + "for task of" + task); }}}); }//threadpool.shutdownnow (); Executors.newscheduledthreadpool (3). Scheduleatfixedrate (New Runnable () {@Overridepublic void run () { System.out.println ("boombing!!");}}, 5, 2, Timeunit.seconDS);}}
Publicclass Testscheduledthreadpoolexecutor { publicstaticvoid main (string[] args) { Scheduledthreadpoolexecutor exec = new Scheduledthreadpoolexecutor (1); Exec.scheduleatfixedrate (New Runnable () {//Every time the exception is triggered @Override publicvoid Run () { //throw new RuntimeException (); System.out.println ("================");}, + , timeunit.milliseconds); Exec.scheduleatfixedrate (New Runnable () {//Print the system time every once in a while to prove that the two do not affect each other @Override publicvoid Run () { System.out.println (System.nanotime ());}}, +, + , timeunit.milliseconds);} }