zoukankan      html  css  js  c++  java
  • 多线程 定时器 Timer TimerTask

    定时器是一种特殊的多线程,使用Timer来安排一次或者重复执行某个任务

     1 package org.zln.thread;
     2 
     3 import java.util.Date;
     4 import java.util.Timer;
     5 import java.util.TimerTask;
     6 
     7 /**
     8  * Created by coolkid on 2015/6/21 0021.
     9  */
    10 public class TestTimerTask extends TimerTask {
    11     @Override
    12     public void run() {
    13         System.out.println(new Date());
    14     }
    15 
    16     public static void main(String[] args) {
    17         Timer timer = new Timer();
    18         TestTimerTask testTimerTask = new TestTimerTask();
    19         timer.schedule(testTimerTask,1000,1000);
    20 
    21     }
    22 }
    E:GitHub oolsJavaEEDevelopLesson1_JavaSe_Demo1srcorgzln hreadTestTimerTask.java

     Timer是一个定时容器,TimerTask子类是具体任务实现类

    小练习:

      间隔一分钟扫描某个目录下是否存在指定文件

     1 package org.zln.thread;
     2 
     3 import java.io.File;
     4 import java.util.Date;
     5 import java.util.Timer;
     6 import java.util.TimerTask;
     7 
     8 /**
     9  * Created by coolkid on 2015/6/21 0021.
    10  */
    11 public class TestTimerTask extends TimerTask {
    12 
    13     private String path;
    14     private boolean flag = true;
    15 
    16     public TestTimerTask(String path) {
    17         this.path = path;
    18     }
    19 
    20     @Override
    21     public void run() {
    22         File file = new File(path);
    23         if (flag&&!file.isDirectory()&&file.exists()){
    24             System.out.println(new Date()+"已检测到文件");
    25             flag = false;
    26         }
    27         if (!flag){
    28             System.out.println(new Date()+"已经检测到文件,无需再次检测");
    29         }
    30         if (flag&&(file.isDirectory()||!file.exists())){
    31             System.out.println(new Date()+"未检测到文件");
    32         }
    33     }
    34 
    35     public static void main(String[] args) {
    36         Timer timer = new Timer();
    37         TestTimerTask testTimerTask = new TestTimerTask("E:\GitHub\tools\实用jar程序\创建或删除标识文件\zln.txt");
    38         timer.schedule(testTimerTask,1000,60000);
    39 
    40     }
    41 }
    E:GitHub oolsJavaEEDevelopLesson1_JavaSe_Demo1srcorgzln hreadTestTimerTask.java

    其他常用Timer方法

      

    Modifier and TypeMethod and Description
    void cancel()
    Terminates this timer, discarding any currently scheduled tasks.
    int purge()
    Removes all cancelled tasks from this timer's task queue.
    void schedule(TimerTask task, Date time)
    Schedules the specified task for execution at the specified time.
    void schedule(TimerTask task, Date firstTime, long period)
    Schedules the specified task for repeated fixed-delay execution, beginning at the specified time.
    void schedule(TimerTask task, long delay)
    Schedules the specified task for execution after the specified delay.
    void schedule(TimerTask task, long delay, long period)
    Schedules the specified task for repeated fixed-delay execution, beginning after the specified delay.
    void scheduleAtFixedRate(TimerTask task, Date firstTime, long period)
    Schedules the specified task for repeated fixed-rate execution, beginning at the specified time.
    void scheduleAtFixedRate(TimerTask task, long delay, long period)
    Schedules the specified task for repeated fixed-rate execution, beginning after the specified delay.
  • 相关阅读:
    python和matlab哪个难?看这篇就够了
    C语言怎么入门?(小白进)
    零基础学习单片机必看的一些知识点
    什么?Windows 里也可以访问 Linux 子系统文件了?
    智能小车开发的重点之电机该如何选型
    8种必知pcb电路原理图的绘制方法,你会吗?
    硬件工程师必备电路设计工具。进来,考考你?
    Leetcode-122. 买卖股票的最佳时机 II
    Leetcode-33. 搜索旋转排序数组
    Leetcode-236. 二叉树的最近公共祖先
  • 原文地址:https://www.cnblogs.com/sherrykid/p/4592363.html
Copyright © 2011-2022 走看看