java倒计时使用java.util.Timer实现,使用两个线程,以秒为单位


public class Countdown3 {
private int lin;
private int curSec;
public Countdown3(int lin)throws InterruptedException{
this.lin = lin;
this.curSec = lin;
System.out.println("最后倒计时:" + lin + "秒");
Timer t = new Timer();
t.schedule(new TimerTask() {
@Override
public void run() {
System.out.println("倒计时:" + --curSec + "秒");
}
},0,1000);
TimeUnit.SECONDS.sleep(lin);
t.cancel();
System.out.println("Game over!!!");
}

public static void main(String[] args) throws InterruptedException{
new Countdown3(20);
}
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM