Home  >  Article  >  Java  >  Description of Java scheduled tasks

Description of Java scheduled tasks

巴扎黑
巴扎黑Original
2017-07-20 13:27:061426browse

1. Introduction

In Java, a complete scheduled task needs to be completed by two classes: Timer and TimerTask. They are defined in the API like this, Timer: a tool that threads use to schedule tasks to be executed in background threads in the future. Tasks can be scheduled to be executed once or to be repeated periodically. By TimerTask: A task scheduled by a Timer for one-time or recurring execution. We can understand that Timer is a timer tool that is used to plan and execute specified tasks on a background thread, while TimerTask is an abstract class, and its subclasses represent a task that can be scheduled by Timer.

Timer Class

In the tool class Timer, four construction methods are provided. Each construction method starts the timer thread. At the same time, the Timer class can ensure that multiple threads can share a single Timer. Object without external synchronization, so the Timer class is thread-safe. However, since each Timer object corresponds to a single background thread, which is used to execute all timer tasks sequentially, generally the time consumed by our thread task execution should be very short, but due to special circumstances, a timer task execution If the time is too long, then it will "exclusively" occupy the timer's task execution thread, and all subsequent threads must wait for it to finish executing, which will delay the execution of subsequent tasks and cause these tasks to pile up. For specific situations, we Analysis later.

When the program initializes the Timer, the scheduled task will be executed according to the time we set. Timer provides the schedule method, which has multiple overloading methods to adapt to different situations, as follows:

Schedule(TimerTask task, Date time): Schedule the execution of the specified task at the specified time.

Schedule(TimerTask task, Date firstTime, long period): Arrange the specified task to start repeated fixed delay execution at the specified time.

Schedule(TimerTask task, long delay): Schedule the execution of the specified task after the specified delay.

Schedule(TimerTask task, long delay, long period): Arranges the specified task for repeated fixed delay execution starting from the specified delay.

At the same time, the scheduleAtFixedRate method is also overloaded. The scheduleAtFixedRate method is the same as schedule, but their focus is different. The difference will be analyzed later.

ScheduleAtFixedRate(TimerTask task, Date firstTime, long period): Arranges the specified task to start repeated fixed-rate execution at the specified time.

ScheduleAtFixedRate(TimerTask task, long delay, long period): Arranges the specified task to start repeated fixed-rate execution after the specified delay.

TimerTask

The TimerTask class is an abstract class, which is arranged by Timer as a task for one-time execution or repeated execution. It has an abstract method run() method which is used to perform the operation to be performed by the corresponding timer task. Therefore, each specific task class must inherit TimerTask and then override the run() method.

In addition, it has two non-abstract methods:

boolean cancel(): Cancel this timer task.

long scheduledExecutionTime(): Returns the scheduled execution time of the most recent actual execution of this task.

2. Example:

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>

Then introduce

<?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>
## in Naims_main.xml

#

The above is the detailed content of Description of Java scheduled tasks. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn