• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Bear Bibeault
  • paul wheaton
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Mikalai Zaikin
  • Piet Souris
Bartenders:

Optimisation of Client/Server communication

 
Ranch Hand
Posts: 40
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 443
3
Eclipse IDE C++ Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 40
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Ranch Hand
Posts: 59
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 40
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sresh Rangi wrote:This can be done with the newer CompletableFuture class.


Good to know. Thank you.
 
He was giving me directions and I was powerless to resist. I cannot resist this tiny ad:
Low Tech Laboratory
https://www.kickstarter.com/projects/paulwheaton/low-tech-0
reply
    Bookmark Topic Watch Topic
  • New Topic