本篇內(nèi)容介紹了“ThreadPoolExecutor線程池的具體用法”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)!專(zhuān)注于網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、成都微信小程序、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶(hù)創(chuàng)新互聯(lián)還提供了江城免費(fèi)建站歡迎大家使用!
ThreadPoolExecutor
ThreadPoolExecutor線程池,java提供開(kāi)發(fā)框架,管理線程的創(chuàng)建、銷(xiāo)毀、優(yōu)化、監(jiān)控等。
有4種不同的任務(wù)隊(duì)列:
1.ArrayBlockingQueue:基于數(shù)組結(jié)構(gòu)的任務(wù)隊(duì)列。此隊(duì)列按先進(jìn)先出的原則對(duì)任務(wù)進(jìn)行排序。
2.LinkedBlockingQueue:基于鏈表結(jié)構(gòu)的任務(wù)隊(duì)列。此隊(duì)列也是按先進(jìn)先出的原則對(duì)任務(wù)進(jìn)行排序。但性能比ArrayBlockingQueue高。
3.synchronousQueue:不存儲(chǔ)元素的任務(wù)隊(duì)列。每個(gè)插入操作必須等到另一個(gè)線程調(diào)用移除操作,否則插入操作一直處于阻塞狀態(tài)。
4.PriorityBlockingQueue:具有優(yōu)先級(jí)的任務(wù)隊(duì)列。此隊(duì)列中的元素必須能夠比較。
拒絕策略:
RejectedExecutionHandler(飽和策略 ):當(dāng)線程池中的線程數(shù)大于maximumPoolSize時(shí),線程池就不能在處理任何任務(wù)了,這時(shí)線程池會(huì)拋出異常。原因就是這個(gè)策略默認(rèn)情況下是AbortPolicy:表示無(wú)法處理新任務(wù)時(shí)拋出異常。
1.AbortPolicy:直接拋出異常。
2.CallerRunsPolicy:只用調(diào)用者所在線程來(lái)運(yùn)行任務(wù)。
3.DiscardOldestPolicy:丟棄隊(duì)列里最近的一個(gè)任務(wù),并執(zhí)行當(dāng)前任務(wù)
4.DiscardPolicy:不處理,丟棄掉。自定義:ThreadPoolExecutor.AbortPolicy()//拋出java.util.concurrent.RejectedExecutionException異常ThreadPoolExecutor.CallerRunsPolicy()//重試添加當(dāng)前的任務(wù),他會(huì)自動(dòng)重復(fù)調(diào)用execute()方法ThreadPoolExecutor.DiscardOldestPolicy()//拋棄舊的任務(wù)ThreadPoolExecutor.DiscardPolicy()// 拋棄當(dāng)前的任務(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)建線程工廠:
用來(lái)創(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è)置為守護(hù)線程, 默認(rèn)為主線程 return thread; }}
線程工具類(lèi):
/** * @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; // 線程池中除了核心線程,其他線程的最大存活時(shí)間 private static int keepAliveTime = 60; // 時(shí)間單位 private static TimeUnit timeUnit = TimeUnit.SECONDS; // 線程等待隊(duì)列 private static BlockingQueue queue = new LinkedBlockingQueue(); //private static BlockingQueue queue = new ArrayBlockingQueue<Runnable>(30); // 創(chuàng)建線程的工廠 private static CheckThreadFactory checkThreadFactory = new CheckThreadFactory("checkGroup"); // 拒絕策略 當(dāng)提交任務(wù)數(shù)超過(guò)maxmumPoolSize+workQueue之和時(shí), // * 即當(dāng)提交第41個(gè)任務(wù)時(shí)(前面線程都沒(méi)有執(zhí)行完,此測(cè)試方法中用sleep(100)), // * 任務(wù)會(huì)交給RejectedExecutionHandler來(lái)處理 /*handler的拒絕策略: 有四種:第一種AbortPolicy:不執(zhí)行新任務(wù),直接拋出異常,提示線程池已滿(mǎn) 第二種DisCardPolicy:不執(zhí)行新任務(wù),也不拋出異常 第三種DisCardOldSetPolicy:將消息隊(duì)列中的第一個(gè)任務(wù)替換為當(dāng)前新進(jìn)來(lái)的任務(wù)執(zhí)行 第四種CallerRunsPolicy:直接調(diào)用execute來(lái)執(zhí)行當(dāng)前任務(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ù)類(lèi),實(shí)現(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ù)類(lèi):
/** * @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); }}
“ThreadPoolExecutor線程池的具體用法”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
網(wǎng)站標(biāo)題:ThreadPoolExecutor線程池的具體用法
標(biāo)題鏈接:http://chinadenli.net/article28/pehhjp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開(kāi)發(fā)、用戶(hù)體驗(yàn)、網(wǎng)站維護(hù)、外貿(mào)建站、App設(shè)計(jì)、Google
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)