我想在后台检查2个整数是否相同。当它们相同时,我希望线程停止,我想要调用我的一个函数。我怎么能这样做?
答案 0 :(得分: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();
}
答案 1 :(得分:0)
试试这个,
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();