Implementing and scheduling a task to be executed by a timer
1) Implement a custom subclass of TimerTask. The run method contains the code that performs the task.
??? class RemindTask extends TimerTask {
??????? public void run() {
??????????? System.out.println("Time up!");
??????????? System.exit(0);
??????? }
??? }
2) Create a thread by instantiating the Timer class
??? Timer timer = new timer();
3) Instantiate the timer task object(new RemindTask())
??? RemindTask task = new RemindTask();
4) Schedule the timer task for execution.
? (1) execute the task after special milliseconds delay.
??? timer.schedule(task,5*1000);
? (2) specify the time when the task suould execute.
??? //execute the task at 11:01 p.m
??? Calendar calendar = Calendar.getInstance();
??? calendar.set(Calendar.HOUR_OF_DAY,23);
??? calendar.set(Calendar.MINUTE,1);
??? calendar.set(Calendar.SECOND,0);
??? Date time = calendar.getTime();
??? timer.schedule(task,time);
Stopping Timer Threads
? By defaulst, aprogram keeps running as long as its timer threads are running. There is four ways to terminate a timer thread
? 1) Invoke cancel on the timer.(timer.cancel())
? 2) Make the timer's thread a daemon(后臺(tái)), by creating the timer like this: new Timer(true). If the only threads left in the program are daemon threads, the program exits.
? 3) After all the timer scheduleds tasks have finished executing,remove all references to the Timer object. the timer thread will terminate.
? 4) Invoke the System.exit method, which makes the entire program and all its threads exit.
Performing a Task repeatedly
There is four Timer method to perform a task repeatedly
? * schedule(TimerTask task, long delay, long period)
??? Schedules the specified task for repeated fixed-delay execution, beginning after the specified delay. Subsequent executions take place at approximately regular intervals separated by the specified period.
??? 執(zhí)行重復(fù)的任務(wù),第一次在延時(shí)時(shí)間后執(zhí)行,往后的以特定的時(shí)間間隔執(zhí)行
??? timer.schedule(new RemindTask(),3*1000,1*1000)
??? RemindTask任務(wù)將會(huì)在3秒后執(zhí)行,以后將會(huì)以1秒的間隔重復(fù)執(zhí)行
? * schedule(TimerTask task, Date time, long period)
??? 執(zhí)行重復(fù)的任務(wù),第一次在特定的時(shí)間執(zhí)行,往后的以特定的時(shí)間間隔執(zhí)行
? * scheduleAtFixedRate(TimerTask task, long delay, long period)
??? Schedules the specified task for repeated fixed-rate execution, beginning after the specified delay. Subsequent executions take place at approximately regular intervals, separated by the specified period.
??? 以固定的延時(shí)執(zhí)行重復(fù)的任務(wù),首次執(zhí)行在特定的延時(shí)之后,以后的執(zhí)行發(fā)生在特定的時(shí)間間隔之后
??? temer.scheduleAtFixedRate(new RemindTask(),3*1000,1*1000)
? * scheduleAtFixedRate(TimerTask task, Date firstTime, long period)
??? 執(zhí)行重復(fù)的任務(wù),第一次在特定的時(shí)間執(zhí)行,往后的以特定的時(shí)間間隔執(zhí)行
? schedule和scheduleAtFixedRate的區(qū)別在于,schedule以固定的相對(duì)時(shí)間間隔執(zhí)行,如果某一次執(zhí)行被延時(shí)了,往后的執(zhí)行的執(zhí)行時(shí)間也會(huì)相對(duì)延時(shí);而scheduleAtFixedRate是以絕對(duì)的時(shí)間間隔執(zhí)行,如果某一次執(zhí)行被延時(shí),它的后一次執(zhí)行的延時(shí)將會(huì)縮短。
The above is the detailed content of Usage of JDK Timer timer. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

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

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

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

Despite the large number of applications that appear every once in a while, Java remains by far one of the most used and important programming languages. Many applications rely on Java on Windows operating systems, and updating it means improving performance by providing stability and security for Java applications to run safely. You can also install Java on Linux and macOS platforms. The only difference is the package/file for each platform. Now, with Windows 11, it’s time to download Java and in today’s article, we will take you through the simple steps to install it on your device. Which version of Java should I download? The Java version you downloaded

The oracle database requires jdk. The reasons are: 1. When using specific software or functions, other software or libraries included in the JDK are required; 2. Java JDK needs to be installed to run Java programs in the Oracle database; 3. JDK provides Develop and compile Java application functions; 4. Meet Oracle's requirements for Java functions to help implement and implement specific functions.

Deepin Linux system is a domestic operating system based on the Linux kernel. It has the characteristics of stability, security, and ease of use. In Deepin Linux system, installing JDK (Java Development Kit) is a necessary step for developing Java applications. This article will introduce in detail how to Install JDK in Deepin Linux system. Installation steps: Open the terminal of Deepin Linux system. Use the command line to download the JDK installation package. The command is as follows: ``shellsudoapt-getinstallopenjdk-11-jdk`` Wait for the download to complete and the system will automatically install the JDK. To verify whether the JDK is installed successfully, enter the following command: ```javaj

The linux jdk directory is in the bin directory. The specific search method is: 1. Find the execution directory of javad through the "whereis java" command; 2. Find the link file through the execution file; 3. Through "ls -lrt /etc/alternatives/java" Just run the command to find the installation directory.

1. Explain that Java provides a dynamic proxy class Proxy. Proxy is not the class of what we call proxy objects, but provides a static method (newProxyInstance) to create proxy objects to obtain proxy objects. 2. Instance publicclassHelloWorld{publicstaticvoidmain(String[]args){//Get the proxy object ProxyFactoryfactory=newProxyFactory();SellTicketsproxyObject=factory.getProxyObject();proxyO

Problem description: When there are multiple jdk in the deployment environment, and the default jdk version is lower than jdk8. When we deploy springboot applications, we need to specify jdk as jdk8 or above. A problem will arise: the external configuration file of the springboot application cannot be loaded, and it will always use the default configuration file imported into the application jar. Problem Solving There are two ways to solve this problem, as follows: Add startup parameters --spring.config.additional-locationnohup/home/jdk1.8.0_251/bin/java-Xms256m-Xmx256m-j

Recently, many friends have asked me how to install jdk. Next, let us learn all about how to install jdk. I hope it can help everyone. 1. First download the JDK installation file and enter the JDK installation interface, as shown in the figure. 2. Click the "Next" button to enter the JDK custom installation interface, as shown in the figure. 3. It is recommended to choose to install directly to the default directory and click the "Next" button to install, as shown in the figure. You can also click the "Change" button to select the installation directory yourself. 4. After the installation is completed, the interface will pop up and click the "Close" button, as shown in the figure. The above is all the tutorials on how to install jdk brought by the editor. I hope it can be helpful to everyone.

The following steps are to deploy the jdk environment in the Linux system 1. Download the jdk installation package 2. Create a new installation jdk folder (/usr/local/java/jdk) cd /usr/localmkdir/usr/local/javalocal directory and create a new java directory cd /usr/local/javamkdir/usr/local/java/jdk Create a new jdk directory cd/usr/local/java/jdk3. Upload the jdk installation package to linux4. Unzip the jdk installation package. Decompression format: The name after the tarzxvf compressed package name can be just Enter an initial letter and use the Tab key
