如何修复我的线程以将线程的初始延迟安排为 2 分钟并且不再安排它。 (即仅安排一次)
@Scheduled(fixedDelay = 5000)
public void myJob() {
Thread.sleep(12000);
}
最佳答案
您可以使用ScheduledExecutorService在这种情况下。它是一个 ExecutorService,可以安排命令在给定的延迟后运行,或定期执行。
ScheduledFuture schedule(Callable callable, long delay, TimeUnit unit)
Creates and executes a ScheduledFuture that becomes enabled after the given delay.
callable - the function to execute
delay - the time from now to delay execution
unit - the time unit of the delay
ScheduledExecutorService service = null;
service = Executors.newScheduledThreadPool(1);
service.schedule(() -> {
myMethodNameWhichIWantToExecute();
}, 2, TimeUnit.MINUTES);
if (service != null) service.shutdown();
关于java - 仅调度线程一次。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53601860/