ThreadPoolExecutor線程池的使用方法
ThreadPoolExecutor
ThreadPoolExecutor線程池,java提供開發(fā)框架,管理線程的創(chuàng)建、銷毀、優(yōu)化、監(jiān)控等。
有4種不同的任務(wù)隊列:
1.ArrayBlockingQueue:基于數(shù)組結(jié)構(gòu)的任務(wù)隊列。此隊列按先進先出的原則對任務(wù)進行排序。
2.LinkedBlockingQueue:基于鏈表結(jié)構(gòu)的任務(wù)隊列。此隊列也是按先進先出的原則對任務(wù)進行排序。但性能比ArrayBlockingQueue高。
3.synchronousQueue:不存儲元素的任務(wù)隊列。每個插入操作必須等到另一個線程調(diào)用移除操作,否則插入操作一直處于阻塞狀態(tài)。
4.PriorityBlockingQueue:具有優(yōu)先級的任務(wù)隊列。此隊列中的元素必須能夠比較。
拒絕策略:
RejectedExecutionHandler(飽和策略 ):當線程池中的線程數(shù)大于maximumPoolSize時,線程池就不能在處理任何任務(wù)了,這時線程池會拋出異常。原因就是這個策略默認情況下是AbortPolicy:表示無法處理新任務(wù)時拋出異常。
1.AbortPolicy:直接拋出異常。
2.CallerRunsPolicy:只用調(diào)用者所在線程來運行任務(wù)。
3.DiscardOldestPolicy:丟棄隊列里最近的一個任務(wù),并執(zhí)行當前任務(wù)
4.DiscardPolicy:不處理,丟棄掉。
自定義:
ThreadPoolExecutor.AbortPolicy()
//拋出java.util.concurrent.RejectedExecutionException異常
ThreadPoolExecutor.CallerRunsPolicy()
//重試添加當前的任務(wù),他會自動重復(fù)調(diào)用execute()方法
ThreadPoolExecutor.DiscardOldestPolicy()
//拋棄舊的任務(wù)
ThreadPoolExecutor.DiscardPolicy()
// 拋棄當前的任務(wù)
private static class RecjectThreadHandler implements RejectedExecutionHandler { @Override public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { } // 異常記錄 private void doLog(Runnable r, ThreadPoolExecutor executor) { System.out.println(r.toString()+"excutor failed."+executor.getCompletedTaskCount()); } }
創(chuàng)建線程工廠:
用來創(chuàng)建線程。
public class CheckThreadFactory implements ThreadFactory { private String threadGroupName; private AtomicInteger count = new AtomicInteger(0); public CheckThreadFactory(String threadGroupName) { this.threadGroupName = threadGroupName; } @Override public Thread newThread(Runnable r) { Thread thread = new Thread(r); thread.setName(threadGroupName+"--"+count.addAndGet(1)); thread.setPriority(5); thread.setDaemon(true);.// 設(shè)置為守護線程, 默認為主線程 return thread; } }
線程工具類:
/** * @author Donald * @create 2019-09-21 21:40 */ public class CheckExcetPool { // 線程池核心線程數(shù) private static int corePoolSize = Runtime.getRuntime().availableProcessors() * 5; // 最大線程數(shù) private static int maximumPoolSize = corePoolSize > 255 ? 255 : corePoolSize * 2; // 線程池中除了核心線程,其他線程的最大存活時間 private static int keepAliveTime = 60; // 時間單位 private static TimeUnit timeUnit = TimeUnit.SECONDS; // 線程等待隊列 private static BlockingQueue queue = new LinkedBlockingQueue(); //private static BlockingQueue queue = new ArrayBlockingQueue<Runnable>(30); // 創(chuàng)建線程的工廠 private static CheckThreadFactory checkThreadFactory = new CheckThreadFactory("checkGroup"); // 拒絕策略 當提交任務(wù)數(shù)超過maxmumPoolSize+workQueue之和時, // * 即當提交第41個任務(wù)時(前面線程都沒有執(zhí)行完,此測試方法中用sleep(100)), // * 任務(wù)會交給RejectedExecutionHandler來處理 /*handler的拒絕策略: 有四種:第一種AbortPolicy:不執(zhí)行新任務(wù),直接拋出異常,提示線程池已滿 第二種DisCardPolicy:不執(zhí)行新任務(wù),也不拋出異常 第三種DisCardOldSetPolicy:將消息隊列中的第一個任務(wù)替換為當前新進來的任務(wù)執(zhí)行 第四種CallerRunsPolicy:直接調(diào)用execute來執(zhí)行當前任務(wù)*/ private static ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor( corePoolSize, maximumPoolSize, keepAliveTime, timeUnit, queue, checkThreadFactory ); public static void submit( Runnable runnable) { System.out.println(corePoolSize+"::"+queue.size()); threadPoolExecutor.submit(runnable); } public static <T> Future submit(Callable<T> callable) { return threadPoolExecutor.submit(callable); } public static <T> void excutor( Runnable run, T result ) { threadPoolExecutor.submit( run,result ); } public static void excutor( Runnable run) { threadPoolExecutor.execute( run); } private static class RecjectThreadHandler implements RejectedExecutionHandler { @Override public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { } // 異常記錄 private void doLog(Runnable r, ThreadPoolExecutor executor) { System.out.println(r.toString()+"excutor failed."+executor.getCompletedTaskCount()); } } }
線程服務(wù)類,實現(xiàn)runnable 接口:
/** * @author Donald * @create 2019-09-21 23:00 */ public class ThreadService implements Runnable { private CountDownLatch countDownLatch; private UserInterface userInterface; public ThreadService(CountDownLatch countDownLatch, UserInterface userInterface) { this.countDownLatch = countDownLatch; this.userInterface = userInterface; } @Override public void run() { try { long start = System.currentTimeMillis(); userInterface.doSomething(); System.err.println(String.format("user time :%s",System.currentTimeMillis()-start)); Thread.sleep(1000); }catch ( Exception e) { e.printStackTrace(); }finally { countDownLatch.countDown(); } } }
具體業(yè)務(wù)邏輯:
/** * @author Donald * @create 2019-09-21 22:51 */ public interface UserInterface { void doSomething(); }
業(yè)務(wù)類:
/** * @author Donald * @create 2019-09-21 22:51 */ public class UserService implements UserInterface { private int number; public UserService(int number) { this.number = number; } @Override public void doSomething() { System.out.println(Thread.currentThread().getName()+"<<<<"+number); } }
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
解決異常:Invalid?keystore?format,springboot配置ssl證書格式不合法問題
這篇文章主要介紹了解決異常:Invalid?keystore?format,springboot配置ssl證書格式不合法問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03Java日志logback的使用配置和logback.xml解讀
這篇文章主要介紹了Java日志logback的使用配置和logback.xml解讀,具有很好的價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06Java中基于DeferredResult的異步服務(wù)詳解
這篇文章主要介紹了Java中基于DeferredResult的異步服務(wù)詳解,DeferredResult字面意思是"延遲結(jié)果",它允許Spring MVC收到請求后,立即釋放(歸還)容器線程,以便容器可以接收更多的外部請求,提升吞吐量,需要的朋友可以參考下2023-12-12使用Spring Security OAuth2實現(xiàn)單點登錄
在本教程中,我們將討論如何使用Spring Security OAuth和Spring Boot實現(xiàn)SSO - 單點登錄。感興趣的朋友跟隨小編一起看看吧2019-06-06java使用BeanUtils.copyProperties踩坑經(jīng)歷
最近在做個項目,踩了個坑特此記錄一下,本文主要介紹了使用BeanUtils.copyProperties踩坑經(jīng)歷,需要的朋友們下面隨著小編來一起學習學習吧2021-05-05java使用異或?qū)崿F(xiàn)變量互換和異或加密解密示例
這篇文章主要介紹了使用異或?qū)崿F(xiàn)變量互換和異或加密解密示例,需要的朋友可以參考下2014-02-02java使用Apache工具集實現(xiàn)ftp文件傳輸代碼詳解
這篇文章主要介紹了java使用Apache工具集實現(xiàn)ftp文件傳輸代碼詳解,分享了詳細連接ftp server和上傳文件,下載文件的代碼,以及結(jié)果展示,具有一定借鑒價值,需要的朋友可以參考下。2017-12-12關(guān)于idea引入spring boot <parent></parent>父依賴標紅問題
這篇文章主要介紹了idea引入spring boot <parent></parent>父依賴標紅問題,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10