Implementing timing Functions in Java

Source: Internet
Author: User

Online Information:

We can use the timer and the TimerTask class to implement timed tasks in Java, which are described in detail below:

1. Basic knowledge
Java.util.Timer
A thread facility that is used to schedule tasks to be performed later in a background thread. You can schedule a task to execute once, or repeat it periodically. This class is thread-safe: Multiple threads can share a single Timer object without having to synchronize externally.
Java.util.TimerTask
A task that is scheduled to be performed or performed repeatedly by a Timer.

2. Sample Code
The example implements a feature that automatically checks the number of connections available in the connection pool and outputs them to the log every 30 minutes during system run time.
First, create a task class that needs to be executed periodically, the task class needs to inherit TimerTask, and then rewrite the run () method, the code in the body of the run () method is the action that needs to be performed, in this demo, we get the number of connections currently available in the connection pool and output to the log. The specific implementation code is as follows:
public class Taskavailableconnectnumber extends TimerTask {
Private Logger log = Logger.getlogger (Taskavailableconnectnumber.class);
Private ConnectionPool pool=connectionpool.getinstance ();
@Override
Publicvoid Run () {
Log.debug ("Number of connections available in the current connection pool" +pool.getavailableconnectnumber ());
}
}
The following defines a listener, which is responsible for opening the timer when the application server starts, the listener needs to implement the Servletcontextlistener interface, and overrides the Contextinitialized () and contextdestroyed () methods, The code is as follows:
public class Onlinelistener implements servletcontextlistener{
Private Logger log = Logger.getlogger (Onlinelistener.class);
Timer timer = null;
This method is executed when the application server starts
Publicvoid contextinitialized (servletcontextevent arg0) {
Create a timer to schedule tasks that need to be performed on a timed basis.
Timer = new timer ();
Schedule a task that needs to be performed regularly for the timer, which is the task class Taskavailableconnectnumber created earlier and specifies that the task is executed every 30 minutes.
Timer.schedule (New Taskavailableconnectnumber (), 0, 30*60*1000);
Log.debug ("Start Timer");
}
When the application server shuts down, the method is executed to complete the shutdown timer operation.
public void contextdestroyed (Servletcontextevent arg0) {
if (timer!=null) {
Timer.cancel ();//Off Timer
Log.debug ("-----timer destroys--------");
}
}
}
The listener needs to be configured in the Web. xml file to run properly, with the following configuration information:
<!--listener Configuration starts-
<listener>
<listener-class>
Cn.sdfi.listen.OnLineListener
</listener-class>
</listener>
<!--listener configuration ends--
Once the above steps are complete, a simple timer is even completed.

Implementing timing Functions in Java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.