Thread Pool, java Thread Pool
To better control multithreading, JDK provides a thread framework Executor to help developers effectively control threads. They are all in the java. util. concurrent package, which is the core of JDK concurrent package.
There is an important class: Executors, which plays the role of the thread factory. We can use Executors to create thread pools for specific functions.
Executors:
NewFixedThreadPool () method. This method returns a fixed number of thread pools. The number of threads of this method remains the same. When a task is submitted, if the thread pool is idle, it is executed immediately,
If no, it will be suspended in a task queue waiting for Idle threads to execute.
NewSingleThreadExecutor () method to create a thread pool. If it is idle, it is executed. If there is no idle thread, it is suspended in the task queue.
The newCachedThreadPool () method returns a thread pool that can adjust the number of threads based on actual conditions, without limiting the maximum number of threads. If there are Idle threads, the task is executed; if no task exists, no thread is created,
In addition, every idle thread is automatically reclaimed 60 seconds later.
NewScheduledThreadPool () method. This method returns a SchededExecutorService object, but the thread pool can specify the number of threads.
Main function:
1 package executor; 2 3 import java. util. concurrent. arrayBlockingQueue; 4 import java. util. concurrent. threadPoolExecutor; 5 import java. util. concurrent. timeUnit; 6 7/** 8 * Created by Administrator on. 9 */10 public class UseThreadPoolExecutor1 {11 12 public static void main (String [] args) {13/*** 14 * when using a bounded queue, if a new task needs to be executed, if the actual number of threads in the thread pool is smaller than corePoolSize, the thread is created first. If the number of threads in the thread pool is greater than corePoolSize, the task is added to the queue. * If the queue is full, a new thread is created when the total number of threads is not greater than maximumPoolSize. 17 * if the number of threads is greater than maximumPoolSize, a denial policy is executed. Or other custom methods. 18 */19 ThreadPoolExecutor pool = new ThreadPoolExecutor (20 1, // coreSize21 2, // MaxSize22 60, // 6023 TimeUnit. SECONDS, 24 new ArrayBlockingQueue <Runnable> (3) // specify a queue (bounded Queue) 25 // new queue blockingqueue <Runnable> () 26, new MyRejected () 27 //, new DiscardOldestPolicy () 28); 29 30 MyTask mt1 = new MyTask (1, "Task 1"); 31 MyTask mt2 = new MyTask (2, "Task 2"); 32 MyTask mt3 = new MyTask (3, "Task 3"); 33 MyTask mt4 = new MyTask (4, "Task 4 "); 34 MyTask mt5 = new MyTask (5, "Task 5"); 35 MyTask mt6 = new MyTask (6, "Task 6"); 36 37 pool.exe cute (mt1 ); 38 pool.exe cute (mt2); 39 pool.exe cute (mt3); 40 pool.exe cute (mt4); 41 pool.exe cute (mt5); 42 pool.exe cute (mt6); 43 44 pool. shutdown (); 45 46} 47}
MyTask class
1 package executor; 2 3 /** 4 * Created by Administrator on 2017/5/17. 5 */ 6 public class MyTask implements Runnable { 7 private int taskId; 8 private String taskName; 9 10 public MyTask(int taskId, String taskName){11 this.taskId = taskId;12 this.taskName = taskName;13 }14 15 public int getTaskId() {16 return taskId;17 }18 19 public void setTaskId(int taskId) {20 this.taskId = taskId;21 }22 23 public String getTaskName() {24 return taskName;25 }26 27 public void setTaskName(String taskName) {28 this.taskName = taskName;29 }30 31 @Override32 public void run() {33 try {34 System.out.println("run taskId =" + this.taskId);35 Thread.sleep(5*1000);36 //System.out.println("end taskId =" + this.taskId);37 } catch (InterruptedException e) {38 e.printStackTrace();39 }40 }41 42 public String toString(){43 return Integer.toString(this.taskId);44 }45 }
MyRejected class:
1 package executor; 2 3 import java. util. concurrent. rejectedExecutionHandler; 4 import java. util. concurrent. threadPoolExecutor; 5 6/** 7 * Created by Administrator on login /5/17. 8 */9 public class MyRejected implements failed {10 11 public MyRejected () {12} 13 14 @ Override15 public void rejectedExecution (Runnable r, ThreadPoolExecutor executor) {16 System. out. println ("custom processing .. "); 17 System. out. println ("the current rejected task is:" + r. toString (); 18 19 20} 21}