Java是一門面向對象編程語言,不僅吸收了C++語言的各種優點,還摒棄了C++里難以理解的多繼承、指針等概念,因此Java語言具有功能強大和簡單易用兩個特征。Java語言作為靜態面向對象編程語言的代表,極好地實現了面向對象理論,允許程序員以優雅的思維方式進行復雜的編程。
Java具有簡單性、面向對象、分布式、健壯性、安全性、平***立與可移植性、多線程、動態性等特點。Java可以編寫桌面應用程序、Web應用程序、分布式系統和嵌入式系統應用程序等。
JDK 自帶的定時器實現
Timer類
這個類允許你調度一個java.util.TimerTask任務。主要有以下幾個方法:
1. schedule(TimerTask task, long delay) 延遲 delay 毫秒 執行
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) 特定時間執行
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 執行并每隔period 執行一次
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 接口實現類
ScheduledExecutorService 是JAVA 1.5 后新增的定時任務接口,主要有以下幾個方法。
- 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
默認實現為ScheduledThreadPoolExecutor 繼承了ThreadPoolExecutor 的線程池特性,配合future特性,比Timer更強大。 具體用法可以閱讀JDK文檔;spring Task內部也是依靠它實現的。示例代碼:
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 定時器實現
Quartz是一個完全由Java編寫的開源作業調度框架,為在Java應用程序中進行作業調度提供了簡單卻強大的機制。Quartz允許開發人員根據時間間隔來調度作業。它實現了作業和觸發器的多對多的關系,還能把多個作業與不同的觸發器關聯。可以動態的添加刪除定時任務,另外很好的支撐集群調度。簡單地創建一個org.quarz.Job接口的Java類,Job接口包含唯一的方法:
public void execute(JobExecutionContext context) throws JobExecutionException;
12
在Job接口實現類里面,添加需要的邏輯到execute()方法中。配置好Job實現類并設定好調度時間表(Trigger),Quartz就會自動在設定的時間調度作業執行execute()。
整合了Quartz的應用程序可以重用不同事件的作業,還可以為一個事件組合多個作業。Quartz通過屬性文件來配置JDBC事務的數據源、全局作業、觸發器偵聽器、插件、線程池等等。(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
創建Job類
public class TestJob implements Job{
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
println(Thread.currentThread().getName() + “ test job begin ” + DateUtil.getCurrentTimeStr());
}
}123456
調度任務
public static void main(String[] args) throws InterruptedException, SchedulerException {
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
// 開始
scheduler.start();
// job 唯一標識 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”)
// 延遲一秒執行
.startAt(new Date(System.currentTimeMillis() + 1000))
// 每隔一秒執行 并一直重復
.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
配置參數的說明
在MONTH和Day Of Week字段里對字母大小寫不敏感
評論