Let's say we have this code:
int number;
public void onMessageReceived(int num) {
number = num;
}
public int getNumber() {
sendMessage("number")
return number;
}
you get the Message only after lets say 1 second.
How do you "wait" until you get the "number" without freezing the Main-Thread?
public int getNumber() {
sendMessage("number")
//WAIT TILL I GET THE "NUMBER"
return number;
}
This kind of asynchronous calls need some callback mechanisms.
You may need to use an observer design pattern that subscribe into an event call.
See this link
Or try to use CallbackTask like in this question:
java-executors-how-to-be-notified-without-blocking-when-a-task-completes