首頁  >  文章  >  Java  >  Java定時任務的描述講解

Java定時任務的描述講解

巴扎黑
巴扎黑原創
2017-07-20 13:27:061390瀏覽

一、簡介

      在Java中一個完整定時任務需要由Timer、TimerTask兩個類別來配合完成。 API中是這樣定義他們的,Timer:一種工具,執行緒用其安排以後在後台執行緒執行的任務。可安排任務執行一次,或定期重複執行。由TimerTask:Timer 安排為一次執行或重複執行的任務。我們可以這樣理解Timer是一種計時器工具,用來在一個後台執行緒計畫執行指定任務,而TimerTask一個抽象類,它的子類別代表一個可以被Timer計畫的任務。

Timer類別

      在工具類別Timer中,提供了四個建構方法,每個建構方法都啟動了計時器線程,同時Timer類別可以保證多個執行緒可以共用單一Timer物件而無需進行外部同步,所以Timer類別是線程安全的。但是由於每個Timer物件對應的是單一後台線程,用於順序執行所有的計時器任務,一般情況下我們的線程任務執行所消耗的時間應該非常短,但是由於特殊情況導致某個定時器任務執行的時間太長,那麼他就會「獨佔」計時器的任務執行線程,其後的所有線程都必須等待它執行完,這就會延遲後續任務的執行,使這些任務堆積在一起,具體情況我們後面分析。

      當程式初始化完成Timer後,定時任務就會按照我們設定的時間去執行,Timer提供了schedule方法,該方法有多中重載方式來適應不同的情況,如下:

      schedule(TimerTask task, Date time):安排在指定的時間執行指定的任務。

      schedule(TimerTask task, Date firstTime, long period) :安排指定的任務在指定的時間開始進行重複的固定延遲執行。

      schedule(TimerTask task, long delay) :安排在指定延遲後執行指定的任務。

      schedule(TimerTask task, long delay, long period) :安排指定的任務從指定的延遲後開始進行重複的固定延遲執行。

      同時也重載了scheduleAtFixedRate方法,scheduleAtFixedRate方法與schedule相同,只不過他們的重點不同,區別後面分析。

      scheduleAtFixedRate(TimerTask task, Date firstTime, long period):安排指定的任務在指定的時間開始進行重複的固定速率執行。

      scheduleAtFixedRate(TimerTask task, long delay, long period):安排指定的任務在指定的延遲後開始進行重複的固定速率執行。

TimerTask

      TimerTask類別是抽象類別,由Timer 安排為一次執行或重複執行的任務。它有一個抽象方法run()方法,該方法用於執行對應計時器任務要執行的操作。因此每一個特定的任務類別都必須繼承TimerTask,然後重寫run()方法。

      另外它有兩個非抽象的方法:

      boolean cancel():取消此計時器任務。

      long scheduledExecutionTime():傳回此任務最近實際執行的安排執行時間。

二、實例:

Naims_task.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:p="http://www.springframework.org/schema/p"
  xmlns:task="http://www.springframework.org/schema/task"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:aop="http://www.springframework.org/schema/aop" 
  xsi:schemaLocation="http://www.springframework.org/schema/beans  
   
   http://www.springframework.org/schema/tx/spring-tx-4.0.xsd  
   http://www.springframework.org/schema/jee/spring-jee-4.0.xsd  
   http://www.springframework.org/schema/context/spring-context-4.0.xsd  
   http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
   http://www.springframework.org/schema/task/spring-task-4.0.xsd"><task:annotation-driven /> <!-- 定时器开关-->
 
  <bean id="myTask" class="com.wisoft.jazwfw.convenienceServices.controller.ConvenienceServicesController"></bean> 
 
  <task:scheduled-tasks> <!-- 这里表示的是每天23点59分执行一次 --><task:scheduled ref="myTask" method="getPubService" cron="0 59 23 * * ?" /> <!-- 这里表示的是每隔十秒执行一次 --><!-- <task:scheduled ref="myTask" method="print" cron="*/10 * * * * ?"/>  -->
  </task:scheduled-tasks> 
 
  <!-- 自动扫描的包名 -->
  <context:component-scan base-package="com.wisoft.jazwfw.convenienceServices.controller" /> 
  </beans>

然後在Naims_main.xml引入

#
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"><beans><import resource="spring/Naims_ws.xml" /><import resource="spring/Naims_bo.xml" /><import resource="spring/Naims_dao.xml"/><import resource="spring/Naims_params.xml"/><import resource="spring/Naims_task.xml"/></beans>

 

#

以上是Java定時任務的描述講解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn