我正在努力制作打开Java制作的小时程序的代码。我对java几乎一无所知。我试着在下面做类似的工作,但它不能像我想的那样工作。
代码:
public static void main(String[] args) throws Exception {
for(int i = 0; i < 5; i++) {
Runtime runtime = Runtime.getRuntime();
String[] s = new String[] {"C:\\Program Files\\BraveSoftware\\Brave-Browser\\Application\\brave.exe"};
Process process = runtime.exec(s);
}
}发布于 2021-12-21 12:36:38
如果您使用的是spring引导,那么可以使用Scheduler注释。
@Scheduled(fixedDelay = 60 * 60 * 1000发布于 2021-12-21 12:47:31
这似乎是一项需要ScheduledExecutorService.的工作根据Java的文档
ScheduledExecutorService接口用调度来补充父ExecutorService的方法,在指定的延迟后执行可运行或可调用的任务。此外,接口定义了scheduleAtFixedRate和scheduleWithFixedDelay,它们以指定的间隔()重复执行指定的任务。
下面是它的Javadoc中的一个例子,它设置了一个ScheduledExecutorService,每十秒钟发出一次声音,持续一个小时:
import static java.util.concurrent.TimeUnit.*;
class BeeperControl {
private final ScheduledExecutorService scheduler =
Executors.newScheduledThreadPool(1);
public void beepForAnHour() {
final Runnable beeper = new Runnable() {
public void run() { System.out.println("beep"); }
};
final ScheduledFuture<?> beeperHandle =
scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS);
scheduler.schedule(new Runnable() {
public void run() { beeperHandle.cancel(true); }
}, 60 * 60, SECONDS);
}
}下面是一个指向StackOverflow answer的链接,该链接使用ScheduledExecutorService在特定时间运行任务。
另一个选择是以CRON作业的形式运行您的任务。这里有一个指向StackOverflow answer的链接,它使用CRON来调度一个Java程序。
发布于 2021-12-21 12:30:46
如果我正确地理解了您的问题,您可以使用Thread.sleep()来空闲,然后启动这个过程。
所以我想像这样的东西会有用的:
public static void main(String[] args) throws InterruptedException, IOException {
ProcessBuilder pb = new ProcessBuilder("path\\to\\file.exe");
while (true) {
pb.start();
Thread.sleep(Duration.ofHours(1).toMillis());
}
}https://stackoverflow.com/questions/70435330
复制相似问题