java多線程開發(fā)ScheduledExecutorService簡化方式

前言
java開發(fā),多多少少會接觸到多線程的應用開發(fā)場景,博主之前做多線程的時候就兩種方式,一個是繼承Thread一個是實現(xiàn)Runnable接口,不過用的多的還是實現(xiàn)Runnable接口,因為實現(xiàn)Runnable接口可以做多線程資源共享!而java.util.concurrent.ScheduledExecutorService接口將大大簡化多線程應用的開發(fā)!也能輕松解決任務定時執(zhí)行的問題!
java多線程的應用場景
應用場景一:
做過微信開發(fā)的都知道,微信的token是有失效時間的,而且每天獲取token都是有限制的,不能每次需要的時候取一次,只能快過期的時候重新去取,這個時候我們就可以給取token的這個操作單獨開個線程,每次取完后線程休眠一段繼續(xù)去取,這樣就保證了token永遠都是有效的 !
應用場景二:
同樣是微信開發(fā)中的問題,微信服務器連我們服務器的時候,必須五秒內(nèi)響應結(jié)果,不然微信就直接提示用戶,該服務不可用了。而我們不可能所有的業(yè)務都能做到五秒內(nèi)完成并響應微信服務器。比如從微信服務器上下載用戶提交的文件保存到我們自己的文件服務器上,(微信服務器臨時保存用戶文件),文件的上傳下載都是比較耗時的操作,我們肯定不能等文件上傳完了在響應微信,所有得單獨開個線程還執(zhí)行文件的下載上傳操作
ScheduledExecutorService方法簡介
/**
*指定delay時間后執(zhí)行任務
* @param command 執(zhí)行的線程任務 Runnable不能返回執(zhí)行結(jié)果
* @param delay 指定某個時間后執(zhí)行
* @param unit 指定時間單位
* @return
*/
public ScheduledFuture schedule(Runnable command, long delay, TimeUnit unit) {
return null;
}
/**
*指定delay時間后執(zhí)行任務
* @param callable 執(zhí)行的線程任務 Callable返回執(zhí)行結(jié)果
* @param delay 指定某個時間后執(zhí)行
* @param unit 指定時間單位
* @param
* @return
*/
publicScheduledFutureschedule(Callablecallable, long delay, TimeUnit unit) {
return null;
}
/**
*等待initiaDelay時間后,每個period時間執(zhí)行一次
* @param command
* @param initialDelay
* @param period
* @param unit
* @return
*/
public ScheduledFuture scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) {
return null;
}
//scheduleWithFixedDelay和scheduleAtFixedRate差不多
public ScheduledFuture scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) {
return null;
}
實例
/**
* @author kl by 2016/5/14
* @boke www.kailing.pub
*/
public class Test {
public static int count=0;
public static void main(String[] args) {
//初始化兩個線城池大小的任務調(diào)度服務
ScheduledExecutorService executorService= Executors.newScheduledThreadPool(2);
//任務一:0秒后開始執(zhí)行,之后每秒執(zhí)行一次
final ScheduledFuture test1= executorService.scheduleAtFixedRate(new Runnable() {
public void run() {
System.out.println("任務一執(zhí)行第"+(count++)+"次 "+Thread.currentThread());
}
},0, 1,TimeUnit.SECONDS);
//任務二:6秒后開始執(zhí)行,并返回執(zhí)行結(jié)果
final ScheduledFuture test2 = executorService.schedule(new Callable() {
public Object call() {
System.out.println("任務二執(zhí)行,傳遞執(zhí)行結(jié)果給任務三 "+Thread.currentThread());
return "任務二已執(zhí)行完,請知曉!";
}
},6,TimeUnit.SECONDS);
//任務三:8秒后執(zhí)行,打印任務二的結(jié)果,終止任務一
executorService.schedule(new Runnable() {
public void run() {
try {
System.out.println(test2.get());
}catch (Exception e){
e.printStackTrace();
}
System.out.println("任務三執(zhí)行,任務一終止 "+Thread.currentThread());
test1.cancel(true);
}
},8,TimeUnit.SECONDS);
System.out.println("我是最先執(zhí)行的嗎?不一定,雖然我是主線程 "+Thread.currentThread());
}
}ps:因為任務三種涉及了任務二的執(zhí)行結(jié)果,所以即使任務三的執(zhí)行時間設置在任務二的執(zhí)行前面,任務三也要等到任務二執(zhí)行完后才能執(zhí)行 ,這個可以修改執(zhí)行時間自己測試測試,體會體會
實例結(jié)果

以上就是java多線程開發(fā)ScheduledExecutorService簡化方式的詳細內(nèi)容,更多關(guān)于java多線程開發(fā)ScheduledExecutorService簡化的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Java?Stream比較兩個List的差異并取出不同的對象四種方法
今天開發(fā)一個需求時要對A和B兩個List集合遍歷,并比較出集合A有,而集合B沒有的值,下面這篇文章主要給大家介紹了關(guān)于Java?Stream比較兩個List的差異并取出不同對象的四種方法,需要的朋友可以參考下2024-01-01
SpringBoot通過ThreadLocal實現(xiàn)登錄攔截詳解流程
這篇文章主要介紹了SpringBoot(HandlerInterceptor)+ThreadLocal實現(xiàn)登錄攔截,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-05-05

