0

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;
}

4 Answers4

1

You probably want to use something like Java Future.

Have a look at the docs or read up this simple example.

Crembo
  • 5,198
  • 3
  • 26
  • 30
  • Look's good! Just what I needed... Going to try it and mark it as correct if it works. – Den Random stuff Jun 09 '15 at 08:40
  • Oh oh.. It still freezes the thread which doesn't me to receive the Message – Den Random stuff Jun 09 '15 at 09:03
  • @DenRandomstuff Maybe you could show the code that you tried? Also, it really depends on the context, i.e. on the app that you're running. If it's a web app, then of course it will need to block at some point. If it's a desktop application, the main UI thread should still be running. – Crembo Jun 09 '15 at 09:19
  • Its a spigot(bukkit) server, everything is on 1 thread which is annoying, but meh – Den Random stuff Jun 09 '15 at 09:31
  • Java Future is usually used when you have a complex computation, and you can basically "start" it at the start of the computation, do lots of other things, and when you actually need the value, you basically block. – Crembo Jun 09 '15 at 09:32
0

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

Community
  • 1
  • 1
iouhammi
  • 1,108
  • 15
  • 29
0

You should use join method...

When a thread calls join() on another thread, the currently running thread will wait until the thread it joins with has completed.

Shailesh Yadav
  • 1,061
  • 1
  • 15
  • 30
-1

May be recursive methodlogy

public int getNumber() {

 while(number!=0)
 getNumber(); //Assuming number will not be null till you get positive integer greater than a zero same method will get recalled.
 sendMessage("number")

return number;
}

When onMessageReceived(int num) called then you can find a positive integer in number so that it will countinue the execution

Rookie007
  • 1,229
  • 2
  • 18
  • 50
  • That would endup spamming the "number" request – Den Random stuff Jun 09 '15 at 08:49
  • Another weakness in your approach: it puts "special meaning" on the value of 0. Long story short: don't try to provide "answers" that just solve the problem. Always consider for yourself the "option space"; and determine the solution that has the best "pro vs. con" ratio. – GhostCat Jun 09 '15 at 08:53
  • @Jägermeister If you can highlight the weakness that would be very helpful :) Will try to find the `pro and con` :) – Rookie007 Jun 09 '15 at 08:58
  • 1
    What I mean is: what if "0" is a perfect valid result coming from `getNumber()`. You **assume** that 0 can be used to distinguish "result received" vs. "still waiting". You are making implicit assumptions here; and that is bad style. – GhostCat Jun 09 '15 at 10:52
  • @Jägermeister I assumed because OP didnt provide much information regarding that, anyhow i will update my answer. Thank you for clarification :) – Rookie007 Jun 09 '15 at 12:33