Introduction to several implementation methods and differences of Java thread pool
import java.text.DateFormat;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.List;import java.util.List;import java.util.concurrent.Callable;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.Future;public class TestThreadPool { // -newFixedThreadPool is similar to cacheThreadPool, and can also be used if it is reuse, but it cannot create new threads at any time // - Its uniqueness: at any point in time, at most, there can be a fixed number of active threads. At this time, if there is a new thread to be established, it can only be placed in another queue and wait until a thread in the current thread terminates and is moved out of the pool directly // - Unlike cacheThreadPool, FixedThreadPool does not have an IDLE mechanism (it may also be there, but since the document does not mention it, it must be very long, similar to TCP or UDP // IDLE mechanism that depends on the upper layer), so FixedThreadPool is mostly aimed at some very stable and fixed regular concurrent threads, mostly used in servers // - From the source code of the method, cache pool and fixed The pool calls the same underlying pool, but the parameters are different: // The fixed pool thread count is fixed, and it is 0 second IDLE (no IDLE) // The cache pool thread count supports 0-Integer.MAX_VALUE (obviously, the host's resource tolerance is not considered at all), 60 second IDLE private static ExecutorService fixedService = Executors.newFixedThreadPool(6); // -Cache pool, first check whether there are previously established threads in the pool, if there are, reuse. If not, create a new thread and add it to the pool // -Cache pool is usually used to perform some asynchronous tasks with very short lifetime // Therefore, it is not used much in some connection-oriented daemon-type SERVERs. // - The thread that can reuse must be a thread in the pool within the timeout IDLE. The default timeout is 60s. If the IDLE time exceeds the duration, the thread instance will be terminated and moved out of the pool. // Note that the thread putting in the CachedThreadPool does not have to worry about its end. If it is inactive after TIMEOUT, it will be automatically terminated. private static ExecutorService cacheService = Executors.newCachedThreadPool(); // -Singleton thread, there can only be one thread in any time pool // -The underlying pool is the same as the cache pool and the fixed pool, but the number of threads is 1-1,0 seconds IDLE (no IDLE) private static ExecutorService singleService = Executors.newSingleThreadExecutor(); // -Scheduled thread pool // -The threads in this pool can be delayed in sequence according to schedule, or the private static ExecutorService scheduledService = Executors.newScheduledThreadPool(10); public static void main(String[] args) { DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); List<Integer> customerList = new ArrayList<Integer>(); System.out.println(format.format(new Date())); testFixedThreadPool(fixedService, customerList); System.out.println("--------------------------"); testFixedThreadPool(fixedService, customerList); fixedService.shutdown(); System.out.println(fixedService.isShutdown()); System.out.println("----------------------------------------------------"); testCacheThreadPool(cacheService, customerList); System.out.println("----------------------------------------------------"); testCacheThreadPool(cacheService, customerList); cacheService.shutdownNow(); System.out.println("----------------------------------------------------"); testSingleServiceThreadPool(singleService, customerList); testSingleServiceThreadPool(singleService, customerList); singleService.shutdown(); System.out.println("----------------------------------------------------"); testScheduledServiceThreadPool(scheduledService, customerList); testScheduledServiceThreadPool(scheduledService, customerList); scheduledService.shutdown(); } public static void testScheduledServiceThreadPool(ExecutorService service, List<Integer> customerList) { List<Callable<Integer>> listCallable = new ArrayList<Callable<Integer>>(); for (int i = 0; i < 10; i++) { Callable<Integer> callable = new Callable<Integer>() { @Override public Integer call() throws Exception { return new Random().nextInt(10); } }; listCallable.add(callable); } try { List<Future<Integer>> listFuture = service.invokeAll(listCallable); for (Future<Integer> future : listFuture) { Integer id = future.get(); customerList.add(id); } } catch (Exception e) { e.printStackTrace(); } System.out.println(customerList.toString()); } public static void testSingleServiceThreadPool(ExecutorService service, List<Integer> customerList) { List<Callable<List<Integer>>> listCallable = new ArrayList<Callable<List<Integer>>>(); for (int i = 0; i < 10; i++) { Callable<List<Integer>>> callable = new Callable<List<Integer>>() { @Override public List<Integer> call() throws Exception { List<Integer> list = getList(new Random().nextInt(10)); boolean isStop = false; while (list.size() > 0 && !isStop) { System.out.println(Thread.currentThread().getId() + " -- sleep:1000"); isStop = true; } return list; } }; listCallable.add(callable); } try { List<Future<List<Integer>>> listFuture = service.invokeAll(listCallable); for (Future<List<Integer>> future : listFuture) { List<Integer> list = future.get(); customerList.addAll(list); } } catch (Exception e) { e.printStackTrace(); } System.out.println(customerList.toString()); } public static void testCacheThreadPool(ExecutorService service, List<Integer> customerList) { List<Callable<List<Integer>>> listCallable = new ArrayList<Callable<List<Integer>>(); for (int i = 0; i < 10; i++) { Callable<List<Integer>> callable = new Callable<List<Integer>>() { @Override public List<Integer> call() throws Exception { List<Integer> list = getList(new Random().nextInt(10)); boolean isStop = false; while (list.size() > 0 && !isStop) { System.out.println(Thread.currentThread().getId() + " -- sleep:1000"); isStop = true; } return list; } }; listCallable.add(callable); } try { List<Future<List<Integer>>> listFuture = service.invokeAll(listCallable); for (Future<List<Integer>> future : listFuture) { List<Integer> list = future.get(); customerList.addAll(list); } } catch (Exception e) { e.printStackTrace(); } System.out.println(customerList.toString()); } public static void testFixedThreadPool(ExecutorService service, List<Integer> customerList) { List<Callable<List<Integer>>> listCallable = new ArrayList<Callable<List<Integer>>>(); for (int i = 0; i < 10; i++) { Callable<List<Integer>> callable = new Callable<List<Integer>>() { @Override public List<Integer> call() throws Exception { List<Integer> list = getList(new Random().nextInt(10)); boolean isStop = false; while (list.size() > 0 && !isStop) { System.out.println(Thread.currentThread().getId() + " -- sleep:1000"); isStop = true; } return list; } }; listCallable.add(callable); } try { List<Future<List<Integer>>> listFuture = service.invokeAll(listCallable); for (Future<List<Integer>> future : listFuture) { List<Integer> list = future.get(); customerList.addAll(list); } } catch (Exception e) { e.printStackTrace(); } System.out.println(customerList.toString()); } public static List<Integer> getList(int x) { List<Integer> list = new ArrayList<Integer>(); list.add(x); list.add(x * x); return list; }}Use: LinkedBlockingQueue to implement thread pool explanation
//For example: corePoolSize=3,maximumPoolSize=6,LinkedBlockingQueue(10)//RejectedExecutionHandler default processing method is: ThreadPoolExecutor.AbortPolicy//ThreadPoolExecutor executorService = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, 1L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(10));//1. If the thread running in the thread pool (that is, calling executorService.execute) does not reach LinkedBlockingQueue.init(10), the number of threads currently executed is: corePoolSize(3) //2. If the number of threads exceeded LinkedBlockingQueue.init(10) and exceeded >=init(10)+corePoolSize(3), and is less than init(10)+maximumPoolSize. The number of threads currently started is: (current threads-init(10))//3. If the number of threads called exceeds init(10)+maximumPoolSize This is handled according to the rules of RejectedExecutionHandler.
About: Explanation of several default implementations of RejectedExecutionHandler
//Default is used: ThreadPoolExecutor.AbortPolicy. The handler is rejected and will throw a runtime RejectedExecutionException. RejectedExecutionHandler policy=new ThreadPoolExecutor.AbortPolicy();////In ThreadPoolExecutor.CallerRunsPolicy, the thread calls the execute itself that runs the task. This strategy provides a simple feedback control mechanism that can slow down the submission of new tasks. //policy=new ThreadPoolExecutor.CallerRunsPolicy();//// In ThreadPoolExecutor.DiscardPolicy, tasks that cannot be executed will be deleted. //policy=new ThreadPoolExecutor.DiscardPolicy();////In ThreadPoolExecutor.DiscardOldestPolicy, if the executor has not been closed, the task at the head of the work queue will be deleted, and the execution of the program will be retry (repeat this process if it fails again). //policy=new ThreadPoolExecutor.DiscardOldestPolicy();
The above introduction to the implementation methods and differences of Java thread pool is all the content I have shared with you. I hope you can give you a reference and I hope you can support Wulin.com more.