I have a main thread which will call the below process
method from a different class..
Now what I am want to do is, if i=1
then, I will make a call to checkValue
method and if it returns false
, then I would like to sleep for two minutes and then again make a call to checkValue
method, and if it still returns false
, then again sleep for two minutes, and then again try checkValue
method but now suppose if it returns true
, then I would go for i=2
iteration, otherwise not.
public void process(String workflowName) {
..// 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;
}
Is there any way to do this?
I would like to go for i=2
for loop iteration only when checkValue
method returns true.. So in short, I need to keep on calling checkValue
method until it returns true every 2 minutes.
Once it returns true, I won't call checkValue
method for i=1
, but for i=2
, I will do the same thing again.
Thread.sleep(millis)
suspends the current thread.
So you have to call Thread.sleep(2 * 60 * 1000)
in the main thread, to sleep for two minutes.
int i = 1;
while ( i <=10 ){
try{
if(!checkValue()){
Thread.sleep(120000);
} else{
i++;
}
} catch (Exception e) {
e.printStackTrace();
}
}
Use break if you don't want to continue the loop.
for (int i = 1; i <= 10; i++) {
try {
.. // some other code here
if (!checkValue()) {
Thread.sleep(2000 * 60);
}
else {
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.