Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Optimisation of Client/Server communication  RSS feed

 
Greg Pata
Ranch Hand
Posts: 31
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Greg Pata
Ranch Hand
Posts: 31
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Sresh Rangi
Ranch Hand
Posts: 54
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Greg Pata
Ranch Hand
Posts: 31
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sresh Rangi wrote:This can be done with the newer CompletableFuture class.

Good to know. Thank you.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!