The system starts a new thread at a higher cost, and using the thread pool can improve performance when you need to create a lot of short-lived threads in your program.
Similar to the database connection pool, the thread pool is created at system startup and creates a large number of idle threads. The program passes a Runnable object to the thread pool, and the thread initiates a thread to execute the object's Run method. When the Run method finishes executing, the thread does not die, but instead returns the thread pool as idle, waiting for the run method of the next Runnable object to execute.
Java provides a executors factory class to produce a thread pool that contains several static factory methods to create the thread pool:
- Newcachedthreadpool (): Creates a thread pool with caching capabilities, and the system creates threads as needed, which are cached in the thread pool;
- Newfixedthreadpool (int nthreads): Creates a reusable thread pool with a fixed number of threads;
- Newsinglethreadexecutor (): Creates a thread pool that is single thread, equivalent to the Newfixedthreadpool parameter passing in 1;
- Newscheduledthreadpool (int corepoolsize): Creates a thread pool with the specified number of threads, which can execute a threading task after a specified delay. Corepoolsize refers to the number of threads held in the pool;
- Newsinglethreadscheduledexecutor (): Creates a thread pool with only one threads that can perform tasks after a specified delay.
The first three methods in the above 5 methods return a Executorservice object that represents a thread pool that can execute the threads represented by the Runnable object and the callable object. The latter two methods return a Scheduledexecutorservice object, which is a subclass of Executorservice that can perform thread tasks after a specified delay.
The Executorservice class provides a Submit method to perform the thread task and returns the execution result (if any); Scheduledexecutorservice provides additional schedule, Scheduleatfixedrate and Scheduleatfixeddelay methods to delay the execution of thread tasks.
When a thread pool is exhausted, the shutdown () method of the thread pool should be called, which initiates a shutdown sequence for the thread pool, and the thread pool that calls the shutdown () method will no longer accept new tasks, but will finish processing the previously received task. When all the tasks in the thread pool are completed, all the threads in the pool die. Alternatively, you can call the thread pool's Shutdownnow () method to close the thread pool, which tries to stop all the tasks that are executing, pauses processing of the awaited task, and returns a list of waiting tasks.
To perform a thread task using the thread pool, follow these steps:
- Call the static factory method of the executor class to create a Executorservice object that represents a thread pool;
- Create an instance of the Runnable or callable interface implementation class to perform the task as a thread;
- Call Excutorservice's Submit method to submit a runnable or callable instance;
- Call Excutorservice's shutdown method to close the thread pool when you do not want to commit any tasks.
ImportJava.util.concurrent.ExecutorService;Importjava.util.concurrent.Executors; Public classThreadPool { Public Static voidMain (string[] args) {Executorservice pool= Executors.newfixedthreadpool (3); Pool.submit (NewMyThread ()); Pool.submit (NewMyThread ()); Pool.submit (NewMyThread ()); Pool.shutdown (); }}classMyThreadImplementsrunnable{Static intCount = 0; Public voidrun () { for(inti=0; i<10; i++) {System.out.println (Thread.CurrentThread (). GetName ()+ "-------" + i + "------" + count++); } }}
Java Multithreading-thread pool