多线程学习--写一个简单的定时器

来源:互联网 发布:linux运行安卓程序 编辑:程序博客网 时间:2024/05/16 11:12
package Demo2;
//开源的工具quartz定义周一到周五执行周末不执行


import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;


public class TraditionalTimerTest {
   private  static int count =0;
public static void main(String args[]){

/* new Timer().schedule(new TimerTask() {

@Override
public void run() {
System.out.println("bombing!!");

}
}, 10000,3000);
while(true){
System.out.println(new Date().getSeconds());
  try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}*/

/*while(true){
System.out.println(new Date().getSeconds());
  try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}*/
/*   new Timer().schedule(new TimerTask() {
@Override
public void run() {
System.out.println("bombing!!");
new Timer().schedule(new TimerTask() {

@Override
public void run() {
System.out.println("bombing!!");
}
}, 2000);
}
}, 2000);*/
//2秒执行4秒执行循环第一种方法
class MyTaskTimer extends TimerTask{
@Override
public void run() {
count=(count+1)%2;
System.out.println("bombing!!");
new Timer().schedule(new MyTaskTimer(), 2000+2000*count);
}
}
new Timer().schedule(new MyTaskTimer(), 2000);
  while(true){
  System.out.println(new Date().getSeconds());
  try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
  }
}
}
0 0
原创粉丝点击