Home > Java > javaTutorial > java multithreading--the use of timer Timer

java multithreading--the use of timer Timer

高洛峰
Release: 2016-12-16 13:56:07
Original
1538 people have browsed it

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:

java multithreading--the use of timer TimerTimer has so many methods:

java multithreading--the use of timer TimerNow 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);
        
    }
}
Copy after login

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);
    }
}
Copy after login

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.


For more java multi-threading-timer usage related articles, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template