7.2. Asynchronous API Benefits
The asynchronous API does not block, which provides multiple benefits, such as:
- The guarantee of synchronous communication, with the added ability to handle failures and exceptions.
- Not being required to block a thread's operations until the call completes.
These benefits allow you to better harness the parallelism in your system, for example:
Set<Future<?>> futures = new HashSet<Future<?>>(); futures.add(cache.putAsync("key1", "value1")); futures.add(cache.putAsync("key2", "value2")); futures.add(cache.putAsync("key3", "value3"));
In the example, The following lines do not block the thread as they execute:
futures.add(cache.putAsync(key1, value1));
futures.add(cache.putAsync(key2, value2));
futures.add(cache.putAsync(key3, value3));
The remote calls from the three put operations are executed in parallel. This is particularly useful when executed in distributed mode. The three keys are pushed to three different nodes in the cluster.