package cn.xxs;
import java.util.Timer;
import java.util.TimerTask;
/**
* java演示倒计时
* xxs
*/
public class CountDown {
public static long midTime;
public static void main(String[] args) {
midTime = 60;
// time1();//方式一
time2();// 方式二
}
/**
* 方式1
*/
private static void time1() {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
midTime--;
long ss = midTime % 60;
System.out.println("还剩" + ss + "秒");
}
}, 0, 1000);
}
/**
* 方式2
*/
private static void time2() {
while (midTime > 0) {
midTime--;
long ss = midTime % 60;
System.out.println("还剩" + ss + "秒");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
结果: