Description of Java scheduled tasks
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>
#
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The Terror Corridor is a mission in Goat Simulator 3. How can you complete this mission? Master the detailed clearance methods and corresponding processes, and be able to complete the corresponding challenges of this mission. The following will bring you Goat Simulator. 3 Horror Corridor Guide to learn related information. Goat Simulator 3 Terror Corridor Guide 1. First, players need to go to Silent Hill in the upper left corner of the map. 2. Here you can see a house with RESTSTOP written on the roof. Players need to operate the goat to enter this house. 3. After entering the room, we first go straight forward, and then turn right. There is a door at the end here, and we go in directly from here. 4. After entering, we also need to walk forward first and then turn right. When we reach the door here, the door will be closed. We need to turn back and find it.

To automate tasks and manage multiple systems, mission planning software is a valuable tool in your arsenal, especially as a system administrator. Windows Task Scheduler does the job perfectly, but lately many people have reported operator rejected request errors. This problem exists in all iterations of the operating system, and even though it has been widely reported and covered, there is no effective solution. Keep reading to find out what might actually work for other people! What is the request in Task Scheduler 0x800710e0 that was denied by the operator or administrator? Task Scheduler allows automating various tasks and applications without user input. You can use it to schedule and organize specific applications, configure automatic notifications, help deliver messages, and more. it

Goat Simulator 3 is a game with classic simulation gameplay, allowing players to fully experience the fun of casual action simulation. The game also has many exciting special tasks. Among them, the Goat Simulator 3 Imperial Tomb task requires players to find the bell tower. Some players are not sure how to operate the three clocks at the same time. Here is the guide to the Tomb of the Tomb mission in Goat Simulator 3! The guide to the Tomb of the Tomb mission in Goat Simulator 3 is to ring the bells in order. Detailed step expansion 1. First, players need to open the map and go to Wuqiu Cemetery. 2. Then go up to the bell tower. There will be three bells inside. 3. Then, in order from largest to smallest, follow the familiarity of 222312312. 4. After completing the knocking, you can complete the mission and open the door to get the lightsaber.

Rescue Steve is a unique task in Goat Simulator 3. What exactly needs to be done to complete it? This task is relatively simple, but we need to be careful not to misunderstand the meaning. Here we will bring you the rescue of Steve in Goat Simulator 3 Task strategies can help you better complete related tasks. Goat Simulator 3 Rescue Steve Mission Strategy 1. First come to the hot spring in the lower right corner of the map. 2. After arriving at the hot spring, you can trigger the task of rescuing Steve. 3. Note that there is a man in the hot spring. Although his name is Steve, he is not the target of this mission. 4. Find a fish named Steve in this hot spring and bring it ashore to complete this task.

TikTok, as one of the most popular social media platforms at the moment, has attracted a large number of users to participate. On Douyin, there are many fan group tasks that users can complete to obtain certain rewards and benefits. So where can I find Douyin fan club tasks? 1. Where can I view Douyin fan club tasks? In order to find Douyin fan group tasks, you need to visit Douyin's personal homepage. On the homepage, you will see an option called "Fan Club." Click this option and you can browse the fan groups you have joined and related tasks. In the fan club task column, you will see various types of tasks, such as likes, comments, sharing, forwarding, etc. Each task has corresponding rewards and requirements. Generally speaking, after completing the task, you will receive a certain amount of gold coins or experience points.

How to Pause Task Manager Process Updates in Windows 11 and Windows 10 Press CTRL+Window Key+Delete to open Task Manager. By default, Task Manager will open the Processes window. As you can see here, all the apps are endlessly moving around and it can be hard to point them down when you want to select them. So, press CTRL and hold it, this will pause the task manager. You can still select apps and even scroll down, but you must hold down the CTRL button at all times.

Frozen or unresponsive programs are easy to kill from Task Manager. But Microsoft has recently provided users with the facility to terminate these tasks directly from the taskbar. While the option isn't rolled out to everyone, it's easily available if you have the Windows Insider build. Here's everything you need to enable the End Task button and close tasks from the taskbar. How to Get the End Task Button from the Taskbar to Kill Apps Currently, the option to enable the End Task button for taskbar apps is only available as a developer option for users with Windows Insider builds. However, this may change in an upcoming feature update as it will be rolled out to users globally on the stable version. If you still

Achieving task universality is a core issue in the research of basic deep learning models, and is also one of the main focuses in the recent direction of large models. However, in the field of time series, various types of analysis tasks vary greatly. There are prediction tasks that require fine-grained modeling and classification tasks that require extracting high-level semantic information. How to build a unified deep basic model to efficiently complete various timing analysis tasks has not yet been established. To this end, a team from the School of Software of Tsinghua University conducted research on the basic issue of timing change modeling and proposed TimesNet, a task-universal timing basic model. The paper was accepted by ICLR 2023. Author list: Wu Haixu*, Hu Tengge*, Liu Yong*, Zhou Hang, Wang Jianmin, Long Mingsheng Link: https://ope
