我想知道是否有一种形式的计时器可以使用,它不会暂停我的所有代码 Thread.sleep(ms)
不是我需要的,因为它会暂停我的所有代码代码。
编辑:好吧,我想我用词错误了。这是我的编辑:
有没有一种方法可以在不暂停我的 main
方法的情况下测量 Java 中的特定时间?
最佳答案
我喜欢使用Executors
类。它有一个很好的方法,那就是newScheduledThreadPool
。它为您提供了一个 ScheduledExecutorService
实例,其中有很多调度方法。
在这里查看: http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Executors.html
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
scheduler.schedule(threadToExecute, 10, TimeUnit.SECONDS);
此代码将在 10 秒后启动 threadToExecute 线程
,而不暂停主线程。
关于java - Java中的定时器不暂停程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29187591/