DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Asynchronous Functional APIs in Java

Asynchronous Functional APIs in Java

Dmitriy Setrakyan user avatar by
Dmitriy Setrakyan
·
Apr. 21, 11 · Java Zone · Interview
Like (0)
Save
Tweet
6.93K Views

Join the DZone community and get the full member experience.

Join For Free

The major limitation of java.util.concurrent.Future class is lack of a good way to listen to future completion asynchronously. Even though Java futures are asynchronous in nature and meant to eliminate synchronous waits when not needed, the only way to get a future result is to synchronously wait for future completion. In many cases this defeats the purpose and Java futures cannot be used.

To avoid this problem we at GridGain created our own future which allows for asynchronous listening for future subscription. In addition to that we have also resolved the inconveniences of error handling and added 'duration()'method to tell users how long the future took to execute. Here is how you can add a listener to get notified, say for completion of put(..) operation on distributed cache.

...
GridCache<Integer, String> cache = grid.cache();

// Asynchronously put a value into cache.
GridFuture<String> fut = cache.putAsync(1, "1");

// Pass in a closure which will be called whenever future completes.
fut.listenAsync(new GridInClosureX<GridFuture<String>>() {
public void applyx(GridFuture<String> fut) throws GridException {
V previousValue = fut.get();

long duration = fut.duration();

System.out.println("Finished cache put operation [prev=" +
previousValue + ", duration= + duration + "ms]");
}
});
...

 GridFuture is used for all asynchronous operations in GridGain, including both, compute and data grid features. Hope you find it useful.

From http://gridgain.blogspot.com/2011/04/asynchronous-functional-apis-in-java.html

Java (programming language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Suspicious Sortings in Unity, ASP.NET Core, and More
  • Adaptive Change Management: A DevOps Approach to Change Management
  • How To Use Cluster Mesh for Multi-Region Kubernetes Pod Communication
  • Implementing One and Two Way SSL (Mutual Authentication) for MuleSoft Application

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo