I want to check in the background if 2 Integers are the same. When they are the same I want the thread to stop and I want one of my function to be called. How can I do this?
Asked
Active
Viewed 22 times
-3
-
It would be useful to see what you've attempted so far as a [mcve] – OneCricketeer May 22 '18 at 06:18
2 Answers
0
I am going to make an assumption here that you know how to setup and execute java threads in the background. In your runnable, when defining your run() function you can setup a while true loop which then compares the value of the two numbers and breaks the loop if they are equal. if they are not equal it sleeps for x milliseconds before it loops. After the loop you call the function you wanted to call if the values were equal. It should look like this:
public void run() {
while(true){
if(a == b){
break;
}
Thread.sleep(200);
}
myfunction();
}

Karan Shishoo
- 2,402
- 2
- 17
- 32
0
Try this out,
Runnable runnable = new Runnable() {
@Override
public void run() {
if (!runnable.interrupted()) {
if( varOne.equals(varTwo)) {
yourFn();
runnable.interrupted();
} else {
//Do nothing.
}
}
};
new Thread(runnable).start();

chinmayan
- 1,304
- 14
- 13