首页 > 学院 > 开发设计 > 正文

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

2019-11-06 07:28:20
字体:
来源:转载
供稿:网友
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 block				e.printStackTrace();			}	   }	  }}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表