定时器是一种特殊的多线程,使用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. poj_2773_Happy 2006

    Two positive integers are said to be relatively prime to each other if the Great Common Divisor (GCD ...

  2. spring cloud 学习之路由网关(zuul)

    学习自方志朋的博客 http://blog.csdn.net/forezp/article/details/69939114 在微服务架构中,需要几个基础的服务治理组件,包括服务注册与发现.服务消费. ...

  3. vue项目中缓存问题

    单页面应用总是存在缓存问题,特别是在微信端,更新页面之后访问的还是老页面,缓存的问题是因为用户访问的脚本地址并没有改变,浏览器就会读取原来的脚本 网上有几种解决办法,首先列举一下 1.加meta,禁止 ...

  4. vue入门——组件基础todolist

    1. 以下是 todolist 的例子,没有用到组件:下面的3 会通过组件拆分todolist <!DOCTYPE html> <html lang="en"&g ...

  5. echarts重新加载动画

    echarts重新加载动画 var option1 = area_right_top1.getOption();area_right_top1.clear();area_right_top1.setO ...

  6. vertical-align垂直居中

    <div id="content"> <div id="weizi"> 锄禾日当午,<br> 汗滴禾下土.<br> ...

  7. 解决百度编辑器ueditor插入视频网址保存不了问题

    问题:如下图,在百度编辑器中插入视频,视频网址可以识别,但是提交内容后视频却显示不了. 解决:这个问题主要是编辑器中会过滤一些html标签,所以可以给标签添加白名单. 修改 ueditor.confi ...

  8. 【Linux】Nginx无法加载.woff .eot .svg .ttf问题解决

    只需要修改Nginx的vhosts.ini,加上以下代码即可修复该问题 location ~ \.(eot|otf|ttf|woff|woff2|svg)$ { add_header Access-C ...

  9. Mysql错误积累001-load data导入文件数据出现1290错误

    错误出现情景 在cmd中使用mysql命令,学生信息表添加数据.使用load data方式简单批量导入数据. 准备好文本数据: xueshengxinxi.txt 文件  数据之间以tab键进行分割 ...

  10. hadoop生态搭建(3节点)-06.hbase配置

    # http://archive.apache.org/dist/hbase/1.2.4/ # ==================================================== ...