Java實(shí)現(xiàn)定時(shí)器的四種方式
Java實(shí)現(xiàn)定時(shí)器的方法有很多,這篇文章主要介紹一下我知道的幾種方式
方法1:使用Timer和和TimerTask類
1、Timer和TimerTask是java.util包下的類,用于實(shí)現(xiàn)定時(shí)任務(wù)
- 步驟1:創(chuàng)建TimerTask定時(shí)器任務(wù),可以通過匿名內(nèi)部類的方式創(chuàng)建
- 步驟2:創(chuàng)建Timer定時(shí)器,調(diào)用定時(shí)器的方法執(zhí)行定時(shí)器任務(wù)
2、Timer的兩個(gè)方法schedule()和scheduleAtFixedRate()及其重載方法:
- void schedule(TimerTask task, long delay):在指定時(shí)間后執(zhí)行1次任務(wù),其中delay表示時(shí)延,單位是毫秒,設(shè)置為1000,則表示1秒后執(zhí)行一次定時(shí)器任務(wù);
- void schedule(TimerTask task, long delay, long period):指定延遲指定時(shí)間后周期性地執(zhí)行任務(wù)(delay毫秒后,每period毫秒執(zhí)行一次)
- void scheduleAtFixedRate(TimerTask task, long delay, long period):指定延遲指定時(shí)間后周期性地執(zhí)行任務(wù)(delay毫秒后,每period毫秒執(zhí)行一次)
- void scheduleAtFixedRate(TimerTask task, Date firstTime,long period) :從指定日期firstTime開始,每period毫秒執(zhí)行一次任務(wù)
3、案例代碼
public class TimerExample { public static void main(String[] args) { // 創(chuàng)建定時(shí)器 Timer timer = new Timer(); // 創(chuàng)建定時(shí)器任務(wù) TimerTask task = new TimerTask() { @Override public void run() { System.out.println("Hello world!"); } }; timer.schedule(task, 1000); // 1秒后執(zhí)行一次 timer.schedule(task, 2000, 2000); // 兩秒后每?jī)擅雸?zhí)行一次 timer.scheduleAtFixedRate(task, 3000, 3000); // 3秒后每3秒執(zhí)行一次 timer.scheduleAtFixedRate(task, new Date(), 4000); // 每4秒執(zhí)行一次 } }
方法2:使用線程池
其中線程池的方法使用和Timer一樣,TimeUnit是一個(gè)枚舉類型,用于指定時(shí)間單位,有NANOSECONDS(納秒)、MICROSECONDS(微秒)、MILISECONDS(毫秒)、SECONDS(秒)、MINUTE(分鐘)、HOURS(小時(shí))和DAYS(天)。
案例代碼:
public class TimerExample { public static void main(String[] args) { // 創(chuàng)建定時(shí)器任務(wù) TimerTask timerTask = new TimerTask() { @Override public void run() { System.out.println("Hello world!"); } }; ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(2); scheduledThreadPool.schedule(timerTask, 1000, TimeUnit.MILLISECONDS); scheduledThreadPool.scheduleAtFixedRate(timerTask, 1000, 1000, TimeUnit.MILLISECONDS); } }
方法3:使用Spring Task
步驟1:在springBoot啟動(dòng)類上添加@EnableScheduling注解
@EnableScheduling @SpringBootApplication public class SpringbootApplication { public static void main(String[] args) { SpringApplication.run(SpringbootApplication.class, args); } }
步驟2:創(chuàng)建一個(gè)定時(shí)任務(wù)類的bean,在類的方法上使用@Schedule注解,通過注解的cron屬性設(shè)置定時(shí)器的屬性
@Component public class TimerTask { @Scheduled(cron = "0 7 2 26 7 *") public void task() { System.out.println("定時(shí)任務(wù)..."); } }
以上代碼指定在2022年7月26日02:07:00執(zhí)行一次定時(shí)任務(wù),對(duì)cron表達(dá)式感興趣的可以去學(xué)習(xí)了解一下,這里就不介紹了。
方法4:使用Quartz任務(wù)調(diào)度工具
步驟1:在pom.xml中添加quartz的依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-quartz</artifactId> </dependency>
步驟:2:定義Job
public class QuartzJob implements Job { @Override public void execute(JobExecutionContext jobExecutionContext) { // 輸出當(dāng)前時(shí)間 System.out.println(LocalDateTime.now()); } }
步驟3:創(chuàng)建quartz的配置類
@Configuration public class QuartzConfig { @Bean public JobDetail jobDetail() { return JobBuilder.newJob(QuartzJob.class) // 綁定要運(yùn)行的任務(wù)類的類對(duì)象 .withIdentity("job") // 設(shè)置job的名稱 .storeDurably() // 信息持久 // 設(shè)置storeDurably之后,當(dāng)沒有觸發(fā)器指向這個(gè)JobDetail時(shí),JobDetail也不會(huì)從 // Spring容器中刪除,如果不設(shè)置這行,就會(huì)自動(dòng)從Spring容器中刪除 .build(); } @Bean public Trigger trigger() { // 定義Cron表達(dá)式,每10秒觸發(fā)一次 CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0/10 * * * * ?"); return TriggerBuilder.newTrigger() .forJob(jobDetail()) // 綁定JobDetail對(duì)象 .withIdentity("trigger") // 定義觸發(fā)器名稱 .withSchedule(cronScheduleBuilder) // 綁定Cron表達(dá)式 .build(); } }
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
idea打包java可執(zhí)行jar包的實(shí)現(xiàn)步驟
這篇文章主要介紹了idea打包java可執(zhí)行jar包的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12SpringBoot如何實(shí)現(xiàn)Tomcat自動(dòng)配置
這篇文章主要介紹了SpringBoot如何實(shí)現(xiàn)Tomcat自動(dòng)配置,幫助大家更好的理解和學(xué)習(xí)使用SpringBoot框架,感興趣的朋友可以了解下2021-03-03java Springboot實(shí)現(xiàn)多文件上傳功能
這篇文章主要為大家詳細(xì)介紹了java Springboot實(shí)現(xiàn)多文件上傳功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08Java正則驗(yàn)證IP的方法實(shí)例分析【測(cè)試可用】
這篇文章主要介紹了Java正則驗(yàn)證IP的方法,結(jié)合實(shí)例形式對(duì)比分析了網(wǎng)上常見的幾種針對(duì)IP的正則驗(yàn)證方法,最終給出了一個(gè)比較靠譜的IP正則驗(yàn)證表達(dá)式,需要的朋友可以參考下2017-08-08java list與數(shù)組之間的轉(zhuǎn)換詳細(xì)解析
以下是對(duì)java中l(wèi)ist與數(shù)組之間的轉(zhuǎn)換進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過來參考下2013-09-09淺析SpringBoot微服務(wù)中異步調(diào)用數(shù)據(jù)提交數(shù)據(jù)庫(kù)的問題
這篇文章主要介紹了SpringBoot微服務(wù)中異步調(diào)用數(shù)據(jù)提交數(shù)據(jù)庫(kù)的問題,今天本文涉及到的知識(shí)點(diǎn)不難,都是很簡(jiǎn)單的crud操作,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07