多线程 定时器 Timer TimerTask
定时器是一种特殊的多线程,使用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的更多相关文章
- 多线程&定时器Timer&同步&线程通信&ThreadLocal
1.多线程 线程状态分为:新建状态.就绪状态.运行状态.阻塞状态.死亡状态 对象等待池的阻塞状态:运行状态执行了wait方法 对向锁池的阻塞状态:试图获得某个同步锁,已经被其他线程占用,就会放到对象的 ...
- 多线程-定时器Timer
2019-04-1218:03:32 package 多线程.定时器Timer_重要; import java.util.Timer; import java.util.TimerTask; publ ...
- java多线程--定时器Timer的使用
定时的功能我们在手机上见得比较多,比如定时清理垃圾,闹钟,等等.定时功能在java中主要使用的就是Timer对象,他在内部使用的就是多线程的技术. Time类主要负责完成定时计划任务的功能,就是在指定 ...
- Java定时器Timer,TimerTask每隔一段时间随机生成数字
1:java.util.Timer类是一种工具,线程用其安排以后在后台线程中执行的任务.可安排任务执行一次,或者定期重复执行. 2:TimerTask类是由 Timer 安排为一次执行或重复执行的任务 ...
- JAVA多线程提高一:传统线程技术&传统定时器Timer
前面我们已经对多线程的基础知识有了一定的了解,那么接下来我们将要对多线程进一步深入的学习:但在学习之前我们还是要对传统的技术进行一次回顾,本章我们回顾的则是:传统线程技术和传统的定时器实现. 一.传统 ...
- 《JAVA多线程编程核心技术》 笔记:第五章:定时器Timer
一.定时器Timer的使用 1.1 方法schedule(TimerTask task, Date time) 是否过期 执行说明 开始执行时间 time>当前时间(未过期) 在time到达时执 ...
- Java多线程编程(五)定时器Timer
一.定时器Timer的使用 在JDK库中Timer类主要负责计划任务的功能,也就是在指定的时间开始执行某一个任务.Timer类的主要作用就是设置计划任务,但封装任务的类确实TimerTask类,执行计 ...
- [并发编程 - 多线程:信号量、死锁与递归锁、时间Event、定时器Timer、线程队列、GIL锁]
[并发编程 - 多线程:信号量.死锁与递归锁.时间Event.定时器Timer.线程队列.GIL锁] 信号量 信号量Semaphore:管理一个内置的计数器 每当调用acquire()时内置计数器-1 ...
- java 多线程Thread 子类 定时器Timer
定时器Timer, 定时器分类: 1,指定时间指定任务(明天早上8点准时提醒我起床),相当于linux里面的at命令 2,周期性的执行任务(每隔三分钟闹钟响一次),相当于Linux里面的cron命令 ...
随机推荐
- LVS NAT,DR,TUN三种负载原理
负载均衡简单介绍 用通俗的话来说负载均衡,就是通过不同的调度机制将用户的请求分派到后端不同的服务器.缓解服务器的请求压力,实现负载均衡的方案有多种,下面简单说说了解的几种方式: DNS 负载:利用DN ...
- Docker(一):概述
Docker 是什么? Docker是一个开源的应用容器引擎,基于Go语言开发 并遵从Apache2.0协议开源. Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级.可移植的容器中,然后 ...
- 在python中安装basemap
在python中安装basemap 1. 确保python环境安装完毕且已配置好环境变量 2. 安装geos: pip install geos 3. 下载.whl文件: (1)pyproj‑1.9. ...
- python 装饰器 生成及原里
# 装饰器形成的过程 : 最简单的装饰器 有返回值的 有一个参数 万能参数 # 装饰器的作用 # 原则 :开放封闭原则 # 语法糖 :@ # 装饰器的固定模式 #不懂技术 import time # ...
- 初识python 字符串 列表 字典相关操作
python基础(一): 运算符: 算术运算: 除了基本的+ - * / 以外,还需要知道 : // 为取整除 返回的市商的整数部分 例如: 9 // 2 ---> 4 , 9.0 // ...
- Leecode刷题之旅-C语言/python-21.合并两个有序链表
/* * @lc app=leetcode.cn id=21 lang=c * * [21] 合并两个有序链表 * * https://leetcode-cn.com/problems/merge-t ...
- mybatis 打印SQL
如果使用的是application.properties文件,加入如下配置: #打印SQL logging.level.com.jn.ssr.supererscuereporting.dao=debu ...
- intellij idea之git执行打标签(tag)和删除标签
intellij idea 版本为2017.2.6 进入Version Control-->log 1.在之前版本中,右键,新建标签 2.输入标签名称,建议输入版本号的方式 3.push标签 由 ...
- JAVA大作业汇总1
JAVA大作业 代码 ``` package thegreatwork; import javafx.application.; import javafx.scene.control.; impor ...
- Hibernate-ORM:12.Hibernate中的多对多关联关系
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 本篇博客将讲述Hibernate中的多对多关联关系的操作,准备的篇幅较少,望海涵 一,讲述多对多 多对多的关联 ...