gpt4 book ai didi

java - 如何每 2 分钟调用一个方法直到它返回 true?

转载 作者:行者123 更新时间:2023-11-30 11:22:21 26 4
gpt4 key购买 nike

我正在从主线程中的不同类调用下面的 process 方法。

现在我要做的是,在 for 循环的第一次迭代中,我将调用 checkValue 方法并查看它是返回 true 还是 false。如果返回false,那我想睡2分钟,睡完再调用checkValue方法,看返回true还是false,如果返回false,再调用再次 hibernate 两分钟,然后我将尝试再次调用 checkValue 方法,如果它现在返回 true,那么我将进入 for 循环的下一次迭代。

public void process(String name) {

..// some other code here

for (int i = 1; i <= 10; i++) {
try {
.. // some other code here

if (!checkValue()) {
Thread.sleep(120000);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}


private boolean checkValue() throws Exception {
boolean check_nodes = false;
return check_nodes;
}

有什么办法吗?

最佳答案

为什么不简单地使用 while 循环而不是嵌套在 for 循环中的 if block ?

while (!checkValue()) { 
try {
Thread.sleep(SLEEP_VALUE);
} catch (InterruptedException ie) {
// do something if interrupted...
}
}

关于java - 如何每 2 分钟调用一个方法直到它返回 true?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21793374/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com