Java是一門面向?qū)ο?a href="http://www.brongaenegriffin.com/v/tag/1315/" target="_blank">編程語言,不僅吸收了C++語言的各種優(yōu)點,還摒棄了C++里難以理解的多繼承、指針等概念,因此Java語言具有功能強大和簡單易用兩個特征。Java語言作為靜態(tài)面向?qū)ο缶幊陶Z言的代表,極好地實現(xiàn)了面向?qū)ο罄碚?,允許程序員以優(yōu)雅的思維方式進行復(fù)雜的編程。
Java具有簡單性、面向?qū)ο?、分布式、健壯性、安全性、?**立與可移植性、多線程、動態(tài)性等特點。Java可以編寫桌面應(yīng)用程序、Web應(yīng)用程序、分布式系統(tǒng)和嵌入式系統(tǒng)應(yīng)用程序等。
JDK 自帶的定時器實現(xiàn)
Timer類
這個類允許你調(diào)度一個java.util.TimerTask任務(wù)。主要有以下幾個方法:
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) 特定時間執(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 接口實現(xiàn)類
ScheduledExecutorService 是JAVA 1.5 后新增的定時任務(wù)接口,主要有以下幾個方法。
- 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)實現(xiàn)為ScheduledThreadPoolExecutor 繼承了ThreadPoolExecutor 的線程池特性,配合future特性,比Timer更強大。 具體用法可以閱讀JDK文檔;spring Task內(nèi)部也是依靠它實現(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 定時器實現(xiàn)
Quartz是一個完全由Java編寫的開源作業(yè)調(diào)度框架,為在Java應(yīng)用程序中進行作業(yè)調(diào)度提供了簡單卻強大的機制。Quartz允許開發(fā)人員根據(jù)時間間隔來調(diào)度作業(yè)。它實現(xiàn)了作業(yè)和觸發(fā)器的多對多的關(guān)系,還能把多個作業(yè)與不同的觸發(fā)器關(guān)聯(lián)。可以動態(tài)的添加刪除定時任務(wù),另外很好的支撐集群調(diào)度。簡單地創(chuàng)建一個org.quarz.Job接口的Java類,Job接口包含唯一的方法:
public void execute(JobExecutionContext context) throws JobExecutionException;
12
在Job接口實現(xiàn)類里面,添加需要的邏輯到execute()方法中。配置好Job實現(xiàn)類并設(shè)定好調(diào)度時間表(Trigger),Quartz就會自動在設(shè)定的時間調(diào)度作業(yè)執(zhí)行execute()。
整合了Quartz的應(yīng)用程序可以重用不同事件的作業(yè),還可以為一個事件組合多個作業(yè)。Quartz通過屬性文件來配置JDBC事務(wù)的數(shù)據(jù)源、全局作業(yè)、觸發(fā)器偵聽器、插件、線程池等等。(quartz.properties)
通過maven引入依賴(這里主要介紹2.3.0) 注意:shiro-scheduler中依賴的是1.x版本 如果同時使用會沖突
《!-- 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();
// 開始
scheduler.start();
// job 唯一標(biāo)識 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ù)的說明


在MONTH和Day Of Week字段里對字母大小寫不敏感

電子發(fā)燒友App


















評論