We see more timing functions on mobile phones, such as scheduled garbage cleaning, alarm clocks, etc. The timing function mainly uses the Timer object in Java, and it uses multi-threading technology internally.
The Time class mainly The function responsible for completing scheduled tasks is to execute a task at the beginning of a specified time. The function of the Timer class is to set up scheduled tasks, and the class that encapsulates the task content is the TimerTask class. This class is an abstract class, and inheritance is required Implement a run method.
By checking the documentation, we see that Timer has the following constructors:
Timer has so many methods:
Now we use a timer to complete a simple function, that is Three seconds after running the project, "It's time for you to wake up" is printed on the console to simulate the function of an alarm clock:
package com.wang.reflect; import java.util.Timer; import java.util.TimerTask; class MyTask extends TimerTask{ @Override public void run() { System.out.println("您该起床了!!!!"); } } public class TimerDemo { public static void main(String[] args) { //创建定时器对象 Timer t=new Timer(); //在3秒后执行MyTask类中的run方法 t.schedule(new MyTask(), 3000); } }
After running, we found that the console printed out quite a lot of text after three seconds, but although the task was executed It's over, but the process is not destroyed and is still in red status. What's the reason?
Creating a Timer is equivalent to starting a new thread. This new thread is not a daemon thread, so it will keep running.
In Time There is a cancel() method in both the class and the TimerTask class. The function of the
TimerTask class is to clear itself from the task queue (a Timer object can execute multiple Timerask tasks). The function of the Timer class is: Clear all tasks in the task queue.
Write an example below to regularly delete all files in a directory on the specified disk:
package com.wang.reflect; import java.io.File; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Timer; import java.util.TimerTask; class MyTask extends TimerTask{ /** * 定时删除指定位置的文件,(这里以删除f盘下aa文件夹的所有文件为例) */ @Override public void run() { File file=new File("f://aa"); deleteFolder(file); } public void deleteFolder(File file){ File[] files=file.listFiles(); for(File f:files){ if(f.isDirectory()){ //使用递归 deleteFolder(f); }else{ f.delete(); } } file.delete(); } } public class TimerDemo { public static void main(String[] args) throws ParseException { //创建定时器对象 Timer t=new Timer(); String time="2016-04-04 11:26:40"; Date d=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time); t.schedule(new MyTask(), d); } }
Note that if in the above test code, the Date type time I gave, it will be early At the current time, the task task will be executed immediately.

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

This article explores integrating functional programming into Java using lambda expressions, Streams API, method references, and Optional. It highlights benefits like improved code readability and maintainability through conciseness and immutability

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

This article explains Java's NIO API for non-blocking I/O, using Selectors and Channels to handle multiple connections efficiently with a single thread. It details the process, benefits (scalability, performance), and potential pitfalls (complexity,

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

This article details Java's socket API for network communication, covering client-server setup, data handling, and crucial considerations like resource management, error handling, and security. It also explores performance optimization techniques, i


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

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.