Hi,
Here is the code of a kind of client/server communication. It works.
However I am sure there is a better way to notify the client as soon as the message arrives.
Thank you for your help.
1) The code as is has a bug .. msgArrived should be AtomicBoolean or volatile or you may get odd results.
2) wait / notify is probably the simplest solution, your inner class (IDataHandler()) should synchronize on a common object and notify when setting the boolean. Then replace the sleep with synchronizing on the same object and a wait (careful with using 'this' for synchronization as new IDataHandler() is an inner class).
"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
Thank you, Chris, for your help!
1) Ok you are right.
2) I have tried and failed. But now, thanks to you it works . The code follows:
(slightly different from what you described)
This can be done with the newer CompletableFuture class. Libraries like guava have similar classes if Java 8 is not available. Create a CompletableFuture inside the checkout method:
Call inside the callback, and outside it to get its value.