我process
从主线程中的另一个类调用以下方法。
现在我想做的是,在for循环的第一次迭代中,我将调用checkValue
method并查看它返回true还是false。如果返回false,那么我想睡2分钟,然后checkValue
在睡眠后再次调用method以查看它是否返回true或false,如果再次返回false,则我将再次睡眠2分钟,然后我将尝试再次调用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块呢?
while (!checkValue()) {
try {
Thread.sleep(SLEEP_VALUE);
} catch (InterruptedException ie) {
// do something if interrupted...
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句