Java 相当于 javascript 中的 setInterval

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/652389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 13:14:42  来源:igfitidea点击:

Java equivalent of setInterval in javascript

javatiminginterrupt

提问by hasen

Basically I want a function to be called every say, 10 milliseconds.

基本上我想要一个函数每说一次,10 毫秒。

How can I achieve that in Java?

我如何在 Java 中实现这一点?

回答by Bombe

You might want to take a look at Timer.

您可能想看看Timer

回答by Peter Lawrey

You could also use a ScheduleExecutorService.

您还可以使用 ScheduleExecutorService。

回答by Ravi Wallau

I would say you would create a thread and in the thread loop add a System.sleep(10) to make the thread "sleep" for 10 ms before continuing.

我会说你会创建一个线程并在线程循环中添加一个 System.sleep(10) 以使线程在继续之前“休眠”10 毫秒。