java定时器demo
package cn.threadtest.thread; import java.util.Date;
import java.util.Timer;
import java.util.TimerTask; public class TraditionalTimerTest { /**
* @param args
*/ //每隔2秒,4秒炸方法1
// static int count = 0; //每隔2秒,4秒炸方法2
static class MyTimerTask1 extends TimerTask { public void run() { System.out.println("bombing!");
new Timer().schedule(new MyTimerTask2(), 2000); }
} static class MyTimerTask2 extends TimerTask { public void run() { System.out.println("bombing!");
new Timer().schedule(new MyTimerTask1(), 4000); }
}
//每隔2秒,4秒炸方法2结束 public static void main(String[] args) { //1第10秒和第13秒炸
// new Timer().schedule(new TimerTask() {
//
// public void run() {
// // TODO Auto-generated method stub
// System.out.println("bombing!");
// }
// }, 10000,3000); // new Timer().schedule(new TimerTask() {
//
// public void run() {
// // TODO Auto-generated method stub
// System.out.println("bombing!");
// new Timer().schedule(/*new TimerTask(){
// public void run() {
// System.out.println("bombing2!");
// };
// }*/this, 2000);
// }
// }, 2000); // //每隔2秒炸开始
// class MyTimerTask extends TimerTask {
//
// public void run() {
// // TODO Auto-generated method stub
// System.out.println("bombing!");
// new Timer().schedule(/*new TimerTask(){
// public void run() {
// System.out.println("bombing2!");
// };
// }*/new MyTimerTask(), 2000);
//
// }
// }
//new Timer().schedule(new MyTimerTask(), 2000);
//每隔2秒炸结束
// //每隔2秒,4秒炸开始方法1
// class MyTimerTask extends TimerTask {
//
//
// public void run() {
//
// count=(count+1)%2;
//
// // TODO Auto-generated method stub
// System.out.println("bombing!");
// new Timer().schedule(/*new TimerTask(){
// public void run() {
// System.out.println("bombing2!");
// };
// }*/new MyTimerTask(), 2000+2000*count);
//
// }
// }
// new Timer().schedule(new MyTimerTask(), 2000);
//每隔2秒,4秒炸方法1结束 //每隔2秒,4秒炸方法2 new Timer().schedule(new MyTimerTask2(), 2000); //每隔2秒,4秒炸方法2结束 while(true){
System.out.println(new Date().getSeconds());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} } }
java定时器demo的更多相关文章
- java定时器和多线程实践记录
这几天因为需要测试mongodb读写分离的问题,因此写了个定时查询程序,并且用到了多线程,以达到定时启动多个线程查询数据库的效果,下边代码记录备忘: package timmer; import ja ...
- java定时器,Spring定时器和Quartz定时器
一.java定时器的应用 其实java很早就有解决定时器任务的方法了,java提供了了类java.util.TimerTask类基于线程的方式来实现定时任务的操作,然后再提供java.util.Tim ...
- Java定时器:Timer
项目中往往会遇到需要定时的任务,例如订单,当用户在某个规定时间内没有操作订单时,订单状态将会发生改变. 那么在这种情况下,我们会用到定时器. 举例: import java.util.Timer; / ...
- Java定时器应用
在Java多线程中,有的时候,我们需要按照指定间隔时间来执行一些任务,这时,我们就要用到定时器.我们在这里以Java中的Timer定时器为例,演示定时器的应用. 请看下述代码: import java ...
- java定时器实现总结
前言:Java定时器目前主要有3种实现方式:JDK组件,Spring Task,Quartz框架. 1. JDK组件(1) java.util.TimerTask MyTimerTask.java: ...
- 微信公众号Java接入demo
微信公众号Java接入demo 前不久买了一台服务,本来是用来当梯子用的,后来买了一个域名搭了一个博客网站,后来不怎么在上面写博客一直闲着,最近申请了一个微信公众号就想着弄点什么玩玩.周末没事就鼓捣了 ...
- Java定时器TimeTask
package com.alan.timer; import java.util.Calendar;import java.util.Date;import java.util.Timer;impor ...
- Java定时器Timer
Java定时器Timer在JDK库中,Timer类主要负责计划任务的功能,也就是在指定的时开始执行某一个任务.Timer类的主要作用就是设置计划任务,但封装任务的类却是TimerTask类,执行计划任 ...
- Kafka2.4发布——新特性介绍(附Java Api Demo代码)
新功能 允许消费者从最近的副本进行获取 为 Consumer Rebalance Protocol 增加对增量协同重新均衡(incremental cooperative rebalancing)的支 ...
随机推荐
- [14]APUE:API for Mysql
库:/usr/lib64/libmysqlclient.so.#.#... 头文件:/usr/lib64/mysql/mysql.h 一.建立连接 MYSQL *mysql_init(MYSQL *) ...
- Mysql命令增加、修改、删除表字段
alter add 命令用来增加表的字段: alter add命令格式:alter table 表名 add字段 类型 其他:如下所示: ) comment '单位' alter drop 命令删除表 ...
- 27-Ubuntu-远程管理命令-01-关机和重启
关机和重启--shutdown 注: 不指定选项和参数,默认表示1分钟之后关闭电脑 远程维护服务器时,最好不要关闭系统而应该重启 实例1:立刻关机 shutdown -h now 实例2:未带任何参数 ...
- 【牛客网多校第一场】A
题目链接:https://www.nowcoder.com/acm/contest/139/A 题意:大概就是给你0,1,2让你填矩阵问有多少种填法满足 a(i,j)<=a(i+1,j)以及a( ...
- Tesseract&tesseractOCRiOS
安装tesseract在上篇. 1.安装之后默认语言包只有英文包,在github上下载中文简体,链接:https://github.com/tesseract-ocr/tessdata 然后放入tes ...
- 8_InlineHook
1 shellcode低2Gb警告.应使用高2GB 稳定 : 在内核挂钩子: 由于每个进程的低2gb 的数据是不同的:所以 在内核挂钩子 因该把 代码 放在 高 2gb. 方法1(申请): 比如 使用 ...
- bcolz
raise Exception("this is an ex") bcolz总结: 0.需要用bcolz的columns需要为ndarray的列,不能直接拿list去赋值,因为我发 ...
- .net下MVC中使用Tuple分页查询数据
主要是在DAL层写查询分页的代码. 例如DAL层上代码: public Tuple<List<WxBindDto>, int> GetMbersInfo(int start, ...
- typedef void (*funcptr)(void) typedef void (*PFV)(); typedef int32_t (*PFI)();
看到以下代码,不明白查了一下: /** Pointer to Function returning Void (any number of parameters) */ typedef void (* ...
- Tomcat的8080、8005、8009端口
8080是http 1.1 connector,也就是接收处理http请求的端口,如果你只用一个tomcat处理所有请求,可以只使用这个connector. 8005是 tomcat接受关闭指令的端口 ...