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

 package org.zln.thread;

 import java.util.Date;
import java.util.Timer;
import java.util.TimerTask; /**
* Created by coolkid on 2015/6/21 0021.
*/
public class TestTimerTask extends TimerTask {
@Override
public void run() {
System.out.println(new Date());
} public static void main(String[] args) {
Timer timer = new Timer();
TestTimerTask testTimerTask = new TestTimerTask();
timer.schedule(testTimerTask,1000,1000); }
}

E:\GitHub\tools\JavaEEDevelop\Lesson1_JavaSe_Demo1\src\org\zln\thread\TestTimerTask.java

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

小练习:

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

 package org.zln.thread;

 import java.io.File;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask; /**
* Created by coolkid on 2015/6/21 0021.
*/
public class TestTimerTask extends TimerTask { private String path;
private boolean flag = true; public TestTimerTask(String path) {
this.path = path;
} @Override
public void run() {
File file = new File(path);
if (flag&&!file.isDirectory()&&file.exists()){
System.out.println(new Date()+"已检测到文件");
flag = false;
}
if (!flag){
System.out.println(new Date()+"已经检测到文件,无需再次检测");
}
if (flag&&(file.isDirectory()||!file.exists())){
System.out.println(new Date()+"未检测到文件");
}
} public static void main(String[] args) {
Timer timer = new Timer();
TestTimerTask testTimerTask = new TestTimerTask("E:\\GitHub\\tools\\实用jar程序\\创建或删除标识文件\\zln.txt");
timer.schedule(testTimerTask,1000,60000); }
}

E:\GitHub\tools\JavaEEDevelop\Lesson1_JavaSe_Demo1\src\org\zln\thread\TestTimerTask.java

其他常用Timer方法

  

Modifier and Type Method 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.

多线程 定时器 Timer TimerTask的更多相关文章

  1. 多线程&定时器Timer&同步&线程通信&ThreadLocal

    1.多线程 线程状态分为:新建状态.就绪状态.运行状态.阻塞状态.死亡状态 对象等待池的阻塞状态:运行状态执行了wait方法 对向锁池的阻塞状态:试图获得某个同步锁,已经被其他线程占用,就会放到对象的 ...

  2. 多线程-定时器Timer

    2019-04-1218:03:32 package 多线程.定时器Timer_重要; import java.util.Timer; import java.util.TimerTask; publ ...

  3. java多线程--定时器Timer的使用

    定时的功能我们在手机上见得比较多,比如定时清理垃圾,闹钟,等等.定时功能在java中主要使用的就是Timer对象,他在内部使用的就是多线程的技术. Time类主要负责完成定时计划任务的功能,就是在指定 ...

  4. Java定时器Timer,TimerTask每隔一段时间随机生成数字

    1:java.util.Timer类是一种工具,线程用其安排以后在后台线程中执行的任务.可安排任务执行一次,或者定期重复执行. 2:TimerTask类是由 Timer 安排为一次执行或重复执行的任务 ...

  5. JAVA多线程提高一:传统线程技术&传统定时器Timer

    前面我们已经对多线程的基础知识有了一定的了解,那么接下来我们将要对多线程进一步深入的学习:但在学习之前我们还是要对传统的技术进行一次回顾,本章我们回顾的则是:传统线程技术和传统的定时器实现. 一.传统 ...

  6. 《JAVA多线程编程核心技术》 笔记:第五章:定时器Timer

    一.定时器Timer的使用 1.1 方法schedule(TimerTask task, Date time) 是否过期 执行说明 开始执行时间 time>当前时间(未过期) 在time到达时执 ...

  7. Java多线程编程(五)定时器Timer

    一.定时器Timer的使用 在JDK库中Timer类主要负责计划任务的功能,也就是在指定的时间开始执行某一个任务.Timer类的主要作用就是设置计划任务,但封装任务的类确实TimerTask类,执行计 ...

  8. [并发编程 - 多线程:信号量、死锁与递归锁、时间Event、定时器Timer、线程队列、GIL锁]

    [并发编程 - 多线程:信号量.死锁与递归锁.时间Event.定时器Timer.线程队列.GIL锁] 信号量 信号量Semaphore:管理一个内置的计数器 每当调用acquire()时内置计数器-1 ...

  9. java 多线程Thread 子类 定时器Timer

    定时器Timer, 定时器分类: 1,指定时间指定任务(明天早上8点准时提醒我起床),相当于linux里面的at命令 2,周期性的执行任务(每隔三分钟闹钟响一次),相当于Linux里面的cron命令 ...

随机推荐

  1. 子域收集-fierce

    fierce 是使用多种技术来扫描目标主机IP地址和主机名的一个DNS服务器枚举工具.运用递归的方式来工作.它的工作原理是先通过查询本地DNS服务器来查找目标DNS服务器,然后使用目标DNS服务器来查 ...

  2. MySQL Waiting for table metadata lock的解决方法

    最近需要在某一个表中新增字段,使用Sequel Pro 或者Navicat工具都会出现程序没有反应,使用 show processlist 查看,满屏都是 Waiting for table meta ...

  3. PHP令人困惑的strtotime

    经常会有人被strtotime结合-1 month, +1 month, next month的时候搞得很困惑, 然后就会觉得这个函数有点不那么靠谱, 动不动就出问题. 用的时候就会很慌… 比如:今天 ...

  4. symfony 数据库使用(二)

    symfony可以根据数据用已经有表反向生成实体,以3.3.*为例: php bin/console doctrine:mapping:import --force AppBundle xml 从现有 ...

  5. 分享一个工作中遇得到的sql(按每天每人统计拖车次数与小修次数)

    查询每人每天的数据 首先先建表 CREATE TABLE `user` ( `name` ) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CR ...

  6. JENKINS系统的安装部署

    JENKINS 安装使用文档 简介 Jenkins是一个功能强大的应用程序,允许持续集成和持续交付项目,无论用的是什么平台.这是一个免费的源代码,可以处理任何类型的构建或持续集成,集成Jenkins可 ...

  7. WPF中使用定时器的注意事项

    原文:WPF中使用定时器的注意事项 注意事项 要使用System.Windows.Threading.DispatcherTimer,而不能使用System.Timers.Timer. 原因是WPF是 ...

  8. EAS集锦

    前言 之前看过的相关BOS开发文档,整理了一些常用的API,一直没有来得及放上来,现在把整理的文件放上来,以备忘查看,分享.闲话少说,上干货! ps 图片不方便查看的话,可以拖住图片,加载到浏览器新页 ...

  9. JavaScript---设计模式之迭代器模式

    迭代器模式提供一种方法顺序访问一个聚合对象中各个元素,而又不需要暴露该方法中的内部表示. jQuery中我们经常会用到一个each函数就是迭代器模式 作用 为遍历不同的集合结构提供一个统一的接口,从而 ...

  10. SPOJ SUBLEX

    SUBLEX - Lexicographical Substring Search 链接 题意 求第k小的子串.相同的算一个. 分析 建立后缀自动机,在后缀自动机上从一个点经过trans,到另一个点, ...