It might be good example especially for billing api IAP for Android.
public void waitTillAsyncFinishOrTimeout(int tries=3, long spanTime=1000){
boolean success = false;
boolean inProgress = true;
AnyTask provider = new AnyTask(new AnyListener() {
@Override
public void onReady() {
success = true;
inProgress = false;
}
});
synchronized (OBJECT_LOCK) {
int triesCount = 0;
while (inProgress && triesCount < tries) {
try {
OBJECT_LOCK.wait(spanTime);
triesCount ++;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
if (success) {
...
}
}