多线程编程入门(5):传统定时器的使用
2016-07-30 11:36
274 查看
1 一个定时任务实现隔,2秒,4秒,2秒,4秒...定时执行
2 实现方案Apackage cn.itcast.heima2;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class TraditionalTimerTestA {
static class MyTimerTaskA extends TimerTask{
@Override
public void run() {
System.out.println("bombing~~~");
new Timer().schedule(new MyTimerTaskB(),4000);//4秒之后,B执行
}
}
static class MyTimerTaskB extends TimerTask{
@Override
public void run() {
System.out.println("bombing~~~");
new Timer().schedule(new MyTimerTaskA(),2000);//2秒之后,A执行
}
}
public static void main(String[] args) {
new Timer().schedule(new MyTimerTaskA(),2000);//导火索,A执行
while(true){
System.out.println(new Date().getSeconds());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
实现方案B
package cn.itcast.heima2;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class TraditionalTimerTestB {
static class MyTimerTaskA extends TimerTask{
private static int count = 0;
@Override
public void run() {
count = (count+1)%2;//1,0,1,0,1,0....
System.out.println("bombing~~~");
new Timer().schedule(new MyTimerTaskA(),2000+count*2000);//4000,2000,4000,2000,....
}
}
public static void main(String[] args) {
new Timer().schedule(new MyTimerTaskA(),2000);
while(true){
System.out.println(new Date().getSeconds());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
2 实现方案Apackage cn.itcast.heima2;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class TraditionalTimerTestA {
static class MyTimerTaskA extends TimerTask{
@Override
public void run() {
System.out.println("bombing~~~");
new Timer().schedule(new MyTimerTaskB(),4000);//4秒之后,B执行
}
}
static class MyTimerTaskB extends TimerTask{
@Override
public void run() {
System.out.println("bombing~~~");
new Timer().schedule(new MyTimerTaskA(),2000);//2秒之后,A执行
}
}
public static void main(String[] args) {
new Timer().schedule(new MyTimerTaskA(),2000);//导火索,A执行
while(true){
System.out.println(new Date().getSeconds());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
实现方案B
package cn.itcast.heima2;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class TraditionalTimerTestB {
static class MyTimerTaskA extends TimerTask{
private static int count = 0;
@Override
public void run() {
count = (count+1)%2;//1,0,1,0,1,0....
System.out.println("bombing~~~");
new Timer().schedule(new MyTimerTaskA(),2000+count*2000);//4000,2000,4000,2000,....
}
}
public static void main(String[] args) {
new Timer().schedule(new MyTimerTaskA(),2000);
while(true){
System.out.println(new Date().getSeconds());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
相关文章推荐
- Java 内存溢出(java.lang.OutOfMemoryError)的常见情况和处理方式
- 无尽走廊(C语言)
- python thread模块
- VS2013编译cvBlob
- C/C++内存管理详解(转载)
- 中序线索二叉树Java实现
- python之路 RabbitMQ、SQLAlchemy
- linphone-LinphoneProxyConfig.java文件分析
- C语言结构体结合malloc、free使用小例子
- java运行时类信息----原理、API及应用
- golang 实现冒泡排序
- python os.system()和os.popen()
- python笔记
- python 抓取ASP.NET平台信息 __VIEWSTATE
- python之json模块
- google firebase推送之大坑记录
- 【Python自动化】编程控制类型变量顺序化
- python之正则表达式
- C++四本有用书
- 《从信息的角度学编程》Chapter3 第一个程序