首页 技术 正文
技术 2022年11月16日
0 收藏 845 点赞 3,341 浏览 2352 个字

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

上一篇: 多个jar包的合并
下一篇: css引入特殊字体
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,028
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,518
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,365
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,146
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,780
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,858