每X秒运行一次java代码,但如果代码有效,请不要';不要等待时间单位

每X秒运行一次java代码,但如果代码有效,请不要';不要等待时间单位,java,java-8,Java,Java 8,我试图每10分钟运行一个脚本sh,该脚本的目标是在服务器上设置一个应用程序。 所以,我定义了一个方法来测试应用程序是否成功部署到服务器上 我的代码如下:运行脚本sh,在服务器上部署应用程序,等待10分钟,通过调用返回布尔值的方法testDeployment()在服务器上测试部署 for (int i=0; i<=x;i++) { Runtime.getRuntime().exec("cmd.exe /c start "+"\\script.sh"); TimeUnit.SECONDS.sl

我试图每10分钟运行一个脚本sh,该脚本的目标是在服务器上设置一个应用程序。 所以,我定义了一个方法来测试应用程序是否成功部署到服务器上

我的代码如下:运行脚本sh,在服务器上部署应用程序,等待10分钟,通过调用返回布尔值的方法testDeployment()在服务器上测试部署

for (int i=0; i<=x;i++) {
Runtime.getRuntime().exec("cmd.exe /c start "+"\\script.sh");
TimeUnit.SECONDS.sleep(300);
testDeploy=testDep.testDeployment();
}

请提供任何帮助

可能是这样的:

Instant start = Instant.now();

boolean testDeploy = false;

do {
     // ...
     testDeploy = testDep.testDeployment();
} while (!testDeploy && Duration.between(start, Instant.now()).toMinutes() < 10);
Instant start=Instant.now();
布尔testDeploy=false;
做{
// ...
testDeploy=testDep.testDeployment();
}而(!testDeploy&&Duration.between(start,Instant.now()).toMinutes()<10);

exec调用返回一个
Process
对象,您可以使用该对象等待完成并测试返回状态。使用它,而不是猜测它需要多少分钟。当测试结果没有任何后果时,您为什么要进行测试也不清楚;当测试成功时,您将再次测试,当测试失败时,您也将再次测试,但延迟不同。
Instant start = Instant.now();

boolean testDeploy = false;

do {
     // ...
     testDeploy = testDep.testDeployment();
} while (!testDeploy && Duration.between(start, Instant.now()).toMinutes() < 10);