1. What is a thread pool
Creating and destroying objects is very time-consuming
Create object: need to allocate resources such as memory
Destroying objects: Although no programmers need to worry, the garbage collector will keep track and destroy them in the background
For resources that are frequently created and destroyed, and use a lot, such as threads in concurrent situations, have a great impact on performance.
Idea: Create multiple threads, put them in the thread pool, get references directly when they are used, and put them back into the pool when not in use Can avoid frequent creation and destruction, and achieve reuse
Technical case: Thread pool, database connection pool starting from JDK1.5, provides built-in thread pool
Second, the benefits of thread pool
1) Improve the response speed (reduced the time to create a new thread)
2) Reduce resource consumption (reuse threads in the thread pool, do not need to be created every time)
3) Improve thread manageability: avoid unlimited creation of threads, thereby destroying system resources, reducing system stability, even memory overflow or CPU exhaustion
Third, the application of thread pool
1) A large number of threads are required and the time to complete the task is short
2) Demanding performance
3) Accept sudden large numbers of requests
Fourth, use the thread pool to execute a large number of Runnable commands
1 public class pool1 { 2 public static void main (String [] args) { 3 // How to create a thread pool 4 // (1) Create a thread pool, there is only one thread object in the thread pool 5 // ExecutorService pool01 = Executors .newSingleThreadExecutor (); 6 // (2) Create a thread pool with a fixed number of threads in the thread pool 7 ExecutorService pool01 = Executors.newFixedThreadPool (10 ); 8 // (3) Create a thread pool in the thread pool The number of threads can be changed dynamically 9 // ExecutorService poolp1 = Executors.newCachedThreadPool (); 10 for ( int i = 0; i <20; i ++) { 11 final int n = i; 12 Runnable command = new Runnable() { 13 @Override 14 public void run() { 15 System.out.println("开始执行:"+n); 16 try { 17 Thread.sleep(2000); 18 } catch (InterruptedException e) { 19 // TODO Auto-generated catch block 20 e.printStackTrace(); 21 } 22 System.out.println ("End of execution:" + n); 23 } 24 }; 25 // End of task 26 // Hand the task to the thread in the thread pool to execute 27 pool01.execute (command); 28 } 29 // Shut down the thread pool 30 pool01.shutdown (); 31 } 32 }
Five, use the thread pool to perform a large number of Callable tasks
1 public class pool2 { 2 public static void main (String [] args) throws ExecutionException, InterruptedException { 3 // How to create a thread pool 4 // (1) Create a thread pool, there is only one thread object in the thread pool 5 // ExecutorService pool1 = Executors.newSingleThreadExecutor (); 6 // (2) Create a thread pool with a fixed number of threads in the thread pool 7 ExecutorService pool1 = Executors.newFixedThreadPool (10 ); 8 // (3) Create a thread pool, The number of threads in the thread pool can be dynamically changed 9 // ExecutorService pool1 = Executors.newCachedThreadPool (); 10 //Create a collection 11 List <Future> list = new ArrayList <> (); 12 / ** Use the thread pool to perform a large number of Callable tasks * / 13 for ( int i = 0; i <20; i ++ ) { 14 Callable <Integer > task = new Callable <Integer> () { 15 @Override 16 public Integer call () throws Exception { 17 Thread.sleep (2000 ); 18 return ( int ) (Math.random () * 10) +1 ; 19 } 20 }; 21 // End of the task 22 // Turn the task into the thread pool 23 Future f = pool1.submit (task); 24 list.add (f); 25 // System.out.println (f.get () ); 26 } 27 System.out.println ("ok?" ); 28 // Traverse collection 29 for (Future future: list) { 30 System.out.println (future.get ()); 31 } 32 System. out.println ("OK!" ); 33 // Close thread pool 34 pool1.shutdown(); 35 } 36 }