使用TimerTask每隔2秒4秒交替执行程序

来源:互联网 发布:网络视频管理系统 编辑:程序博客网 时间:2024/04/30 07:37
package com.zhlk.thread;import java.util.Date;import java.util.Timer;import java.util.TimerTask;public class TraditionalTimerTest {/** * 创建日期:2017-3-4下午9:32:26 * 作者:lvguanghui */     private static int count=0;public static void main(String[] args) { class MyTimerTask extends TimerTask {  public void run() {  count=(count+1)%2; System.out.println("nihao");       new Timer().schedule(new MyTimerTask(), 2000+2000*count); }}  new Timer().schedule(new MyTimerTask(), 2000);   while(true){System.out.println("第"+new Date().getSeconds()+"秒");  try {Thread.sleep(1000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}   }  }}

0 0
原创粉丝点击