一、java定時任務三種
第一種:
/**
* 普通thread
* 這是最常見的,建立一個thread,然後讓它在while循環裡一直運作着,
* 通過sleep方法來達到定時任務的效果。這樣可以快速簡單的實作,代碼如下:
*/
public class task1 {
public static void main(string[] args) {
// run in a second
final long timeinterval = 1000;
runnable runnable = new runnable() {
public void run() {
while (true) {
// ------- code for task to run
system.out.println("hello lnint !!");
// ------- ends here
try {
thread.sleep(timeinterval);
} catch (interruptedexception e) {
e.printstacktrace();
}
}
}
};
thread thread = new thread(runnable);
thread.start();
}
}
第二種
import java.util.timer;
import java.util.timertask;
/**
*
* 于第一種方式相比,優勢 1>當啟動和去取消任務時可以控制 2>第一次執行任務時可以指定你想要的delay時間
* 在實作時,timer類可以排程任務,timertask則是通過在run()方法裡實作具體任務。 timer執行個體可以排程多任務,它是線程安全的。
* 當timer的構造器被調用時,它建立了一個線程,這個線程可以用來排程任務。 下面是代碼:
public class task2 {
timertask task = new timertask() {
@override
// task to run goes here
system.out.println("hello lnint !!");
timer timer = new timer();
long delay = 0;
long intevalperiod = 1 * 1000;
// schedules the task to be run in an interval
timer.scheduleatfixedrate(task, delay, intevalperiod);
} // end of main
}
第三種
import java.util.concurrent.executors;
import java.util.concurrent.scheduledexecutorservice;
import java.util.concurrent.timeunit;
* scheduledexecutorservice是從java se5的java.util.concurrent裡,做為并發工具類被引進的,這是最理想的定時任務實作方式。
* 相比于上兩個方法,它有以下好處:
* 1>相比于timer的單線程,它是通過線程池的方式來執行任務的
* 2>可以很靈活的去設定第一次執行任務delay時間
* 3>提供了良好的約定,以便設定執行的時間間隔
* 下面是實作代碼,我們通過scheduledexecutorservice#scheduleatfixedrate展示這個例子,通過代碼裡參數的控制,首次執行加了delay時間。
public class task3 {
system.out.println("hello lnint !!");
scheduledexecutorservice service = executors
.newsinglethreadscheduledexecutor();
// 第二個參數為首次執行的延時時間,第三個參數為定時執行的間隔時間
service.scheduleatfixedrate(runnable, 10, 1, timeunit.seconds);
}
二、java web 定時
第一步:
public class timermanager {
//時間間隔
private static final long period_day = 24 * 60 * 60 * 1000;
public timermanager() {
calendar calendar = calendar.getinstance();
/*** 定制每日2:00執行方法 ***/
calendar.set(calendar.hour_of_day, 16);
calendar.set(calendar.minute, 10);
calendar.set(calendar.second, 0);
date date=calendar.gettime(); //第一次執行定時任務的時間
system.out.println(date);
system.out.println("before 方法比較:"+date.before(new date()));
//如果第一次執行定時任務的時間 小于 目前的時間
//此時要在 第一次執行定時任務的時間 加一天,以便此任務在下個時間點執行。如果不加一天,任務會立即執行。循環執行的周期則以目前時間為準
if (date.before(new date())) {
date = this.addday(date, 1);
system.out.println(date);
}
timer timer = new timer();
nfdflightdatatimertask task = new nfdflightdatatimertask();
//安排指定的任務在指定的時間開始進行重複的固定延遲執行。
timer.schedule(task,date,period_day);
}
// 增加或減少天數
public date addday(date date, int num) {
calendar startdt = calendar.getinstance();
startdt.settime(date);
startdt.add(calendar.day_of_month, num);
return startdt.gettime();
}
}
第二步:
/**
* 在 timermanager 這個類裡面,大家一定要注意 時間點的問題。如果你設定在淩晨2點執行任務。但你是在2點以後
*釋出的程式或是重新開機過服務,那這樣的情況下,任務會立即執行,而不是等到第二天的淩晨2點執行。為了,避免這種情況
*發生,隻能判斷一下,如果釋出或重新開機服務的時間晚于定時執行任務的時間,就在此基礎上加一天。
*/
public class nfdflightdatatimertask extends timertask {
private static simpledateformat formatter = new simpledateformat("yyyy-mm-dd hh:mm:ss");
@override
public void run() {
try {
//在這裡寫你要執行的内容
system.out.println("執行目前時間"+formatter.format(calendar.getinstance().gettime()));
} catch (exception e) {
system.out.println("-------------解析資訊發生異常--------------");
}
}
}
第三步:
public class nfdflightdatatasklistener implements servletcontextlistener {
public void contextinitialized(servletcontextevent sce) {
new timermanager();
}
public void contextdestroyed(servletcontextevent sce) {
// todo auto-generated method stub
}
}
第四步:
配置web.xml檔案
<!--nfdflightdatatasklistener 監聽器-->
<listener>
<listener-class>com.eh.util.nfdflightdatatasklistener</listener-class>
</listener>
三、spring定時三種
第一種:
scheduledtimertask: spring的scheduledtimertask定義了一個定時器任務的運作周期,遺憾的是,你可以指定任務執行的頻度,但你無法精确指定它何時運作,這就需要用到第二種quartz進行任務排程;
建立一個業務任務,在spring配置檔案中聲明 ;
在spring 配置檔案中,配置scheduledtimertask ,并且關聯上自定義的任務執行個體;
啟動定時器,spring的timerfactorybean負責啟動定時任務
使用 quartz:還是老樣子定義業務邏輯任務:
在spring中聲明并且配置作業排程的觸發方式:
這裡 quartz的作業觸發器有兩種,分别是
org.springframework.scheduling.quartz.simpletriggerbean
org.springframework.scheduling.quartz.crontriggerbean
1.simpletriggerbean,隻支援按照一定頻度調用任務,如每隔一段時間運作一次。
<bean id="simpletrigger" class="org.springframework.scheduling.quartz.simpletriggerbean">
<property name="jobdetail" ref="myjob" />
<property name="startdelay" value="0" /><!-- 排程工廠執行個體化後,經過0秒開始執行排程 -->
<property name="repeatinterval" value="2000" /><!-- 每2秒排程一次 -->
</bean>
2.crontriggerbean,支援到指定時間運作一次,如每天12:00運作一次,如上配置;
配置排程工廠
org.springframework.scheduling.quartz.schedulerfactorybean,代碼如上;
啟動你的應用即可
第三種:
使用 spring-task
spring自帶的定時任務工具,spring task,可以将它比作一個輕量級的quartz,而且使用起來很簡單,除spring相關的包外不需要額外的包,而且支援注解和配置檔案兩種:
第一步:編寫任務類;taskjob,method job1 --代碼省略
第二步:在spring配置檔案頭中添加命名空間及描述
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:task="http://www.springframework.org/schema/task"
第三步:spring配置檔案中設定具體的任務
<task:scheduled-tasks>
<task:scheduled ref="taskjob" method="job1" cron="0 * * * * ?"/>
</task:scheduled-tasks>
<context:component-scan base-package="com.alibaba.mytask" />
說明:ref參數指定的即任務類,method指定的即需要運作的方法,cron及cronexpression表達式,具體寫法這裡不介紹了,<context:component-scan base-package="com.alibaba.mytask" />spring掃描注解用的。
四、js定時
1.循環執行:每隔一段時間執行
不帶參數:
<script type=”text/javascript”>
//循環執行,每隔3秒鐘執行一次showalert()
window.setinterval(showalert, 3000);
function showalert()
{
alert(“aaaaa”);
}
</script>
帶參數:
<script type=”text/javascript”>
//循環執行,每隔3秒鐘執行一次 showalert()
window.setinterval(function(){
showalert(“aaaaa”);
}, 3000);
function showalert(mess)
alert(mess);
2.定時執行:延時固定時間後去執行一段函數
不帶參數:
//定時執行,5秒後執行show()
window.settimeout(show,5000);
function show()
alert(“bbb”);
帶參數:
//定時執 行,5秒後執行showalert()
window.settimeout(function(){
showalert(“bbbbbb”);
},5000);