最近項(xiàng)目中遇到了一個(gè)新的需求,就是實(shí)現(xiàn)一個(gè)可以動(dòng)態(tài)添加定時(shí)任務(wù)的功能。說(shuō)到這里,有人可能會(huì)說(shuō)簡(jiǎn)單啊,使用quartz就好了,簡(jiǎn)單粗暴。然而quartz框架太重了,小項(xiàng)目根本不好操作啊。當(dāng)然,也有人會(huì)說(shuō),jdk提供了timer的接口啊,完全夠用啊。但是我們項(xiàng)目的需求完全是多線(xiàn)程的模型啊,而timer是單線(xiàn)程的,so,樓主最后還是選擇了jdk的線(xiàn)程池。
在成都網(wǎng)站建設(shè)、做網(wǎng)站中從網(wǎng)站色彩、結(jié)構(gòu)布局、欄目設(shè)置、關(guān)鍵詞群組等細(xì)微處著手,突出企業(yè)的產(chǎn)品/服務(wù)/品牌,幫助企業(yè)鎖定精準(zhǔn)用戶(hù),提高在線(xiàn)咨詢(xún)和轉(zhuǎn)化,使成都網(wǎng)站營(yíng)銷(xiāo)成為有效果、有回報(bào)的無(wú)錫營(yíng)銷(xiāo)推廣。創(chuàng)新互聯(lián)公司專(zhuān)業(yè)成都網(wǎng)站建設(shè)10余年了,客戶(hù)滿(mǎn)意度97.8%,歡迎成都創(chuàng)新互聯(lián)客戶(hù)聯(lián)系。
線(xiàn)程池是什么
Java通過(guò)Executors提供四種線(xiàn)程池,分別為:
newCachedThreadPool :創(chuàng)建一個(gè)可緩存線(xiàn)程池,如果線(xiàn)程池長(zhǎng)度超過(guò)處理需要,可靈活回收空閑線(xiàn)程,若無(wú)可回收,則新建線(xiàn)程。
newFixedThreadPool : 創(chuàng)建一個(gè)定長(zhǎng)線(xiàn)程池,可控制線(xiàn)程最大并發(fā)數(shù),超出的線(xiàn)程會(huì)在隊(duì)列中等待。
newScheduledThreadPool : 創(chuàng)建一個(gè)定長(zhǎng)線(xiàn)程池,支持定時(shí)及周期性任務(wù)執(zhí)行。
newSingleThreadExecutor : 創(chuàng)建一個(gè)單線(xiàn)程化的線(xiàn)程池,它只會(huì)用唯一的工作線(xiàn)程來(lái)執(zhí)行任務(wù),保證所有任務(wù)按照指定順序(FIFO, LIFO, 優(yōu)先級(jí))執(zhí)行。
樓主項(xiàng)目中用到的是newScheduledThreadPool, 就這些吧,再多的樓主就班門(mén)弄斧了,Google一下,一大堆。
線(xiàn)程池service的獲取
樓主通過(guò)單例模式來(lái)獲取線(xiàn)程池的service,代碼如下:
/** * 線(xiàn)程池創(chuàng)建. * @author wuhf * @date 2018/01/16 */ public class ThreadPoolUtils { private static ScheduledExecutorService executorService; private ThreadPoolUtils() { //手動(dòng)創(chuàng)建線(xiàn)程池. executorService = new ScheduledThreadPoolExecutor(10, new BasicThreadFactory.Builder().namingPattern("syncdata-schedule-pool-%d").daemon(true).build()); } private static class PluginConfigHolder { private final static ThreadPoolUtils INSTANCE = new ThreadPoolUtils(); } public static ThreadPoolUtils getInstance() { return PluginConfigHolder.INSTANCE; } public ScheduledExecutorService getThreadPool(){ return executorService; } }
中斷某一個(gè)正在運(yùn)行的線(xiàn)程代碼實(shí)現(xiàn)
廢話(huà)就不多說(shuō)了,代碼如下:
/** * 中斷線(xiàn)程池的某個(gè)任務(wù). */ public class InterruptThread implements Runnable { private int num; public InterruptThread (int num){ this.num = num; } public static void main(String[] args) throws InterruptedException { Thread interruptThread = new Thread(new InterruptThread(1)); ScheduledFuture<?> t = ThreadPoolUtils.getInstance().getThreadPool().scheduleAtFixedRate(interruptThread,0,2, TimeUnit.SECONDS); InterruptThread interruptThread1 = new InterruptThread(2); ThreadPoolUtils.getInstance().getThreadPool().scheduleAtFixedRate(interruptThread1,0,2, TimeUnit.SECONDS); InterruptThread interruptThread2 = new InterruptThread(3); ThreadPoolUtils.getInstance().getThreadPool().scheduleAtFixedRate(interruptThread2,0,2, TimeUnit.SECONDS); Thread.sleep(5000); //終止正在運(yùn)行的線(xiàn)程interruptThread t.cancel(true); while (true){ } } @Override public void run() { System.out.println("this is a thread" + num); } }
踩坑記錄
樓主在使用如下代碼時(shí),突然想到當(dāng)這個(gè)定時(shí)任務(wù)需要被停止時(shí)該如何停止線(xiàn)程運(yùn)行
ThreadPoolUtils.getInstance().getThreadPool().scheduleAtFixedRate(interruptThread,0,2, TimeUnit.SECONDS);
既然我有這樣的需求,那就Google一下吧,找了大半圈,愣是沒(méi)找到相關(guān)資料,都是一些關(guān)于Java線(xiàn)程池的深入分析。或者是全局變量啥的,并沒(méi)有找到令樓主滿(mǎn)意的解決方案。
既然沒(méi)有線(xiàn)程的那就扒一下scheduleAtFixedRate的底層源碼看看是什么東西吧,果不其然我在源碼中看到了scheduleAtFixedRate方法的具體實(shí)現(xiàn),發(fā)現(xiàn)他的返回值是ScheduledFuture。
public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) { if (command == null || unit == null) throw new NullPointerException(); if (period <= 0) throw new IllegalArgumentException(); ScheduledFutureTask<Void> sft = new ScheduledFutureTask<Void>(command, null, triggerTime(initialDelay, unit), unit.toNanos(period)); RunnableScheduledFuture<Void> t = decorateTask(command, sft); sft.outerTask = t; delayedExecute(t); return t; }
接著往下我們?cè)倏纯碨cheduledFuture里面有什么東西吧,沒(méi)有讓樓主失望,看到了這個(gè)
public boolean cancel(boolean mayInterruptIfRunning) { boolean cancelled = super.cancel(mayInterruptIfRunning); if (cancelled && removeOnCancel && heapIndex >= 0) remove(this); return cancelled; } //從線(xiàn)程的運(yùn)行隊(duì)列中移除當(dāng)前線(xiàn)程 public boolean remove(Runnable task) { boolean removed = workQueue.remove(task); tryTerminate(); // In case SHUTDOWN and now empty return removed; }
再往上查super.cancel(mayInterruptIfRunning)是什么東西,我們看到了這個(gè),
//通過(guò)調(diào)用線(xiàn)程的interrupt方法終止線(xiàn)程運(yùn)行 public boolean cancel(boolean mayInterruptIfRunning) { if (!(state == NEW && UNSAFE.compareAndSwapInt(this, stateOffset, NEW, mayInterruptIfRunning ? INTERRUPTING : CANCELLED))) return false; try { // in case call to interrupt throws exception if (mayInterruptIfRunning) { try { Thread t = runner; if (t != null) t.interrupt(); } finally { // final state UNSAFE.putOrderedInt(this, stateOffset, INTERRUPTED); } } } finally { finishCompletion(); } return true; }
到這里所有的問(wèn)題都迎刃而解。
總結(jié)一下吧
項(xiàng)目中總是會(huì)遇到比較難搞的解決方案,當(dāng)Google不太好找時(shí),翻一下jdk的源碼或許也是一個(gè)不錯(cuò)的方法。
文章標(biāo)題:Java實(shí)現(xiàn)終止線(xiàn)程池中正在運(yùn)行的定時(shí)任務(wù)
URL分享:http://newbst.com/article30/jhshpo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、ChatGPT、品牌網(wǎng)站設(shè)計(jì)、網(wǎng)站設(shè)計(jì)公司、全網(wǎng)營(yíng)銷(xiāo)推廣、營(yíng)銷(xiāo)型網(wǎng)站建設(shè)
聲明:本網(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)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)