本文介绍了我想为游戏java实现一个计时器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要一个方法 startTimer(30)
其中参数是倒计时的秒数。我如何用Java做到这一点?
I want to have a method startTimer(30)
where the parameter is the amount of seconds to countdown. How do I do so in Java?
推荐答案
Java 5这样做的方式如下:
The Java 5 way of doing this would be something like:
void startTimer(int delaySeconds) {
Executors.newSingleThreadScheduledExecutor().schedule(
runnable,
delaySeconds,
TimeUnit.SECONDS);
}
runnable
描述你想做什么。例如:
The runnable
describes what you want to do. For example:
Runnable runnable = new Runnable() {
@Override public void run() {
System.out.println("Hello, world!");
}
}
这篇关于我想为游戏java实现一个计时器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!