我需要实现一个函数,在点击一个按钮60秒后运行。请帮帮忙,我使用了Timer类,但我认为这不是最好的方法。
发布于 2018-11-24 04:26:13
使用Java9 CompletableFuture,非常简单:
CompletableFuture.delayedExecutor(5, TimeUnit.SECONDS).execute(() -> {
// Your code here executes after 5 seconds!
});
发布于 2019-05-21 00:53:57
new Timer().schedule(new TimerTask() {
@Override
public void run() {
// here goes your code to delay
}
}, 300L); // 300 is the delay in millis
Here你可以找到一些信息和例子。
发布于 2014-10-11 13:42:28
您可以简单地使用Thread.sleep()
来实现此目的。但是,如果您在具有用户界面的多线程环境中工作,则需要在单独的线程中执行此操作,以避免睡眠阻塞用户界面。
try{
Thread.sleep(60000);
// Then do something meaningful...
}catch(InterruptedException e){
e.printStackTrace();
}
https://stackoverflow.com/questions/26311470
复制相似问题