Java是一門面向?qū)ο?a href="http://m.xsypw.cn/v/tag/1315/" target="_blank">編程語(yǔ)言,不僅吸收了C++語(yǔ)言的各種優(yōu)點(diǎn),還摒棄了C++里難以理解的多繼承、指針等概念,因此Java語(yǔ)言具有功能強(qiáng)大和簡(jiǎn)單易用兩個(gè)特征。Java語(yǔ)言作為靜態(tài)面向?qū)ο缶幊陶Z(yǔ)言的代表,極好地實(shí)現(xiàn)了面向?qū)ο罄碚?,允許程序員以優(yōu)雅的思維方式進(jìn)行復(fù)雜的編程。
Java具有簡(jiǎn)單性、面向?qū)ο?、分布式、健壯性、安全性、?**立與可移植性、多線程、動(dòng)態(tài)性等特點(diǎn)。Java可以編寫桌面應(yīng)用程序、Web應(yīng)用程序、分布式系統(tǒng)和嵌入式系統(tǒng)應(yīng)用程序等。
JDK 自帶的定時(shí)器實(shí)現(xiàn)
Timer類
這個(gè)類允許你調(diào)度一個(gè)java.util.TimerTask任務(wù)。主要有以下幾個(gè)方法:
1. schedule(TimerTask task, long delay) 延遲 delay 毫秒 執(zhí)行
public static void main(String[] args) {
for (int i = 0; i 《 10; ++i) {
new Timer(“timer - ” + i).schedule(new TimerTask() {
@Override
public void run() {
println(Thread.currentThread().getName() + “ run ”); }
}, 1000);
}
}
out :
timer - 2 run
timer - 1 run
timer - 0 run
timer - 3 run
timer - 9 run
timer - 4 run
timer - 8 run
timer - 5 run
timer - 6 run
timer - 7 run
?
2. schedule(TimerTask task, Date time) 特定時(shí)間執(zhí)行
public static void main(String[] args) {
for (int i = 0; i 《 10; ++i) {
new Timer(“timer - ” + i).schedule(new TimerTask() {
@Override
public void run() {
println(Thread.currentThread().getName() + “ run ”);
}
}, new Date(System.currentTimeMillis() + 2000));
}
}
out:
timer - 0 run
timer - 7 run
timer - 6 run
timer - 8 run
timer - 3 run
timer - 5 run
timer - 2 run
timer - 1 run
timer - 4 run
timer - 9 run
?
3. schedule(TimerTask task, long delay, long period) 延遲 delay 執(zhí)行并每隔period 執(zhí)行一次
public static void main(String[] args) {
for (int i = 0; i 《 10; ++i) {
new Timer(“timer - ” + i).schedule(new TimerTask() {
@Override
public void run() {
println(Thread.currentThread().getName() + “ run ”);
}
}, 2000 , 3000);
}
}
out:
timer - 0 run
timer - 5 run
timer - 4 run
timer - 8 run
timer - 3 run
timer - 2 run
timer - 1 run
timer - 7 run
timer - 9 run
timer - 6 run
timer - 3 run
timer - 7 run
timer - 5 run
timer - 4 run
timer - 8 run
ScheduledExecutorService 接口實(shí)現(xiàn)類
ScheduledExecutorService 是JAVA 1.5 后新增的定時(shí)任務(wù)接口,主要有以下幾個(gè)方法。
- ScheduledFuture《?》 schedule(Runnable command,long delay, TimeUnit unit);
- 《V》 ScheduledFuture《V》 schedule(Callable《V》 callable,long delay, TimeUnit unit);
- ScheduledFuture《?》 scheduleAtFixedRate(Runnable command,long initialDelay,long period,TimeUnitunit);
- ScheduledFuture《?》 scheduleWithFixedDelay(Runnable command,long initialDelay,long delay,TimeUnitunit);1234
默認(rèn)實(shí)現(xiàn)為ScheduledThreadPoolExecutor 繼承了ThreadPoolExecutor 的線程池特性,配合future特性,比Timer更強(qiáng)大。 具體用法可以閱讀JDK文檔;spring Task內(nèi)部也是依靠它實(shí)現(xiàn)的。示例代碼:
public static void main(String[] args) throws SchedulerException {
ScheduledThreadPoolExecutor executor = (ScheduledThreadPoolExecutor)Executors.newScheduledThreadPool(10);
for (int i = 0; i 《 10; ++i) {
executor.schedule(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + “ run ”);
}
} , 2 , TimeUnit.SECONDS);
}
executor.shutdown();
}
out:
pool-1-thread-2 run
pool-1-thread-5 run
pool-1-thread-4 run
pool-1-thread-3 run
pool-1-thread-8 run
pool-1-thread-5 run
pool-1-thread-7 run
pool-1-thread-2 run
pool-1-thread-1 run
pool-1-thread-6 run
Quartz 定時(shí)器實(shí)現(xiàn)
Quartz是一個(gè)完全由Java編寫的開(kāi)源作業(yè)調(diào)度框架,為在Java應(yīng)用程序中進(jìn)行作業(yè)調(diào)度提供了簡(jiǎn)單卻強(qiáng)大的機(jī)制。Quartz允許開(kāi)發(fā)人員根據(jù)時(shí)間間隔來(lái)調(diào)度作業(yè)。它實(shí)現(xiàn)了作業(yè)和觸發(fā)器的多對(duì)多的關(guān)系,還能把多個(gè)作業(yè)與不同的觸發(fā)器關(guān)聯(lián)。可以動(dòng)態(tài)的添加刪除定時(shí)任務(wù),另外很好的支撐集群調(diào)度。簡(jiǎn)單地創(chuàng)建一個(gè)org.quarz.Job接口的Java類,Job接口包含唯一的方法:
public void execute(JobExecutionContext context) throws JobExecutionException;
12
在Job接口實(shí)現(xiàn)類里面,添加需要的邏輯到execute()方法中。配置好Job實(shí)現(xiàn)類并設(shè)定好調(diào)度時(shí)間表(Trigger),Quartz就會(huì)自動(dòng)在設(shè)定的時(shí)間調(diào)度作業(yè)執(zhí)行execute()。
整合了Quartz的應(yīng)用程序可以重用不同事件的作業(yè),還可以為一個(gè)事件組合多個(gè)作業(yè)。Quartz通過(guò)屬性文件來(lái)配置JDBC事務(wù)的數(shù)據(jù)源、全局作業(yè)、觸發(fā)器偵聽(tīng)器、插件、線程池等等。(quartz.properties)
通過(guò)maven引入依賴(這里主要介紹2.3.0) 注意:shiro-scheduler中依賴的是1.x版本 如果同時(shí)使用會(huì)沖突
《!-- https://mvnrepository.com/artifact/org.quartz-scheduler/quartz --》
《dependency》
《groupId》org.quartz-scheduler《/groupId》
《artifactId》quartz《/artifactId》
《version》2.3.0《/version》
《/dependency》123456
創(chuàng)建Job類
public class TestJob implements Job{
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
println(Thread.currentThread().getName() + “ test job begin ” + DateUtil.getCurrentTimeStr());
}
}123456
調(diào)度任務(wù)
public static void main(String[] args) throws InterruptedException, SchedulerException {
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
// 開(kāi)始
scheduler.start();
// job 唯一標(biāo)識(shí) test.test-1
JobKey jobKey = new JobKey(“test” , “test-1”);
JobDetail jobDetail = JobBuilder.newJob(TestJob.class).withIdentity(jobKey).build();
Trigger trigger = TriggerBuilder.newTrigger()
.withIdentity(“test” , “test”)
// 延遲一秒執(zhí)行
.startAt(new Date(System.currentTimeMillis() + 1000))
// 每隔一秒執(zhí)行 并一直重復(fù)
.withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(1).repeatForever())
.build();
scheduler.scheduleJob(jobDetail , trigger);
Thread.sleep(5000);
// 刪除job
scheduler.deleteJob(jobKey);
}
out :
DefaultQuartzScheduler_Worker-1test job begin 2017-06-03 14:30:33
DefaultQuartzScheduler_Worker-2test job begin 2017-06-03 14:30:34
DefaultQuartzScheduler_Worker-3test job begin 2017-06-03 14:30:35
DefaultQuartzScheduler_Worker-4test job begin 2017-06-03 14:30:36
DefaultQuartzScheduler_Worker-5test job begin 2017-06-03 14:30:37
配置參數(shù)的說(shuō)明
在MONTH和Day Of Week字段里對(duì)字母大小寫不敏感
評(píng)論