我从主线程中的另一个类调用下面的process
方法。
现在我要做的是,在for循环的第一次迭代中,我将调用checkValue
方法并查看它是返回true还是false。如果它返回false,那么我想睡2分钟,然后在睡觉后再次调用checkValue
方法,看它是否返回true或false,如果返回false,则再次调用两分钟然后我会尝试再次调用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;
}
有没有办法做到这一点?
答案 0 :(得分:1)
为什么不简单地使用while循环而不是嵌套在for循环中的if块?
while (!checkValue()) {
try {
Thread.sleep(SLEEP_VALUE);
} catch (InterruptedException ie) {
// do something if interrupted...
}
}