DZone
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
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Java
  4. Google Guava – Futures

Google Guava – Futures

Bill Bejeck user avatar by
Bill Bejeck
·
Dec. 02, 11 · Interview
Like (0)
Save
Tweet
Share
15.46K Views

Join the DZone community and get the full member experience.

Join For Free

This post is a continuation of my series on Google Guava, this time covering Futures. The Futures class is a collection of static utility methods for working with the Future/ListenableFuture interface. A Future is a handle to an asynchronous task, either a Runnable or Callable, that was submitted to an ExecutorService. The Future interface provides methods for: getting the results of a task, checking if a task is done, or canceling a task. The ListenableFuture interface extends the Future interface and adds the ability to set a completion listener to run once a task is finished. To create a ListenableFuture you first need to decorate an ExecutorService instance like so:

ExecutorService executorService = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool());

Now all submitted Callables/Runnables will return a ListenableFuture. MoreExecutors can be found in the com.google.common.util.concurrent package. ListenableFutures were covered in the previous post. There are too many methods in Futures to effectively cover in one post, so I am only going to cover: chain, transform, allAsList and successfulAsList. Throughout this post, I will refer to Futures and ListenableFutures interchangeably.

Chain

The chain method returns a ListenableFuture whose value is calculated by taking the results from an input Future and applying it as an argument a Function object, which in turn, returns another ListenableFuture. Let’s look at a code example and step through it:

ListenableFuture<List<String>> indexSearch =
              luceneSearcher.searchAsync("firstName:martin");

Function<List<String>, ListenableFuture<List<Person>>> queryFunction =
new Function<List<String>, ListenableFuture<List<Person>>>() {
            @Override
            public ListenableFuture<List<Person>> apply(final List<String> ids) {
                 return dataService.getPersonsByIdAsync(ids);
            }
        };

ListenableFuture<List<Person>> results =
              Futures.chain(indexSearch, queryFunction,executorService);
  1. Line 1 is executing an asynchronous search using Lucene and will return a list of id’s that represent the primary key of a person record stored in a database. (I created a small index where the only data stored in Lucene is the id’s and the rest of the data was indexed only).
  2. Lines 4 – 11 are building the function object where the apply method will use the results from the search future as input. The future returned from apply is a result of the call to the dataService object.
  3. Line 12 is the future that is returned from the chain call. The executorService is used to run the function once the input future is completed.

For additional clarity, here is what the searchAsync and getPersonsByIdAsync methods are doing. These method calls are from lines 2 and 8 respectively, in the previous code example:

public ListenableFuture<List<String>> searchAsync(final String query)  {
	        return executorService.submit(new Callable<List<String>>() {
	            @Override
	            public List<String> call() throws Exception {
	                return search(query);
	            }
	        });
  }

public ListenableFuture<List<Person>> getPersonsByIdAsync(final List<String> ids) {
        return executorService.submit(new Callable<List<Person>>() {
            @Override
            public List<Person> call() throws Exception {
                return getPersonsById(ids);
            }
        });
    }

The chain method has two signatures:

  1. chain(ListentableFuture, Function)
  2. chain(ListenableFuture, Function, ExecutorService)

When determining which method to use there are a few points to consider.
If the input future is completed by the time chain is called, the supplied function will be executed immediately in the calling thread. Additionally, if no executor is provided then a MoreExecutors.sameThreadExecutor is used. MoreExecutors.sameThreadExecutor (as the name implies) follows the ThreadPoolExecutor.CallerRunsPolicy, meaning the submitted task is run in the same thread that called execute/submit.

Transform

The transform method is similar to the chain method, in that it takes a Future and Function object as arguments. The difference is that a ListenableFuture is not returned, just the results of applying the given function to the results of the input future. Consider the following:

List<String> ids = ....
ListenableFuture<List<Map<String, String>>> dbRecords =
                                           dataService.getPersonDataByIdAsync(ids);

 Function<List<Map<String, String>>,List<Person>> transformDbResults =
  new Function<List<String>, List<Person>>() {
              @Override
              public List<Person> apply(List<Map<String, String>> personMapList) {
                      List<Person> personObjList = new ArrayList<Person>();
                      for(Map<String,String> personDataMap : personMapList){
                             personObjList.add(new Person(personDataMap);
                      }
                      return personObjList;
                    }
     };

ListenableFuture<List<Person>> transformedResults =
                      Futures.transform(dbRecords, transformDbResults, executorService);
  1. On line 2 an asynchronous database lookup is performed
  2. On line 4 a function object is being created, but on line 8, notice the return type is List<Person>

The transform method has the same overloaded method calls as chain, with the same caveats.

AllAsList

The allAsList method will take an arbitrary number of ListenableFutures either as varargs or in the form of an Iterator<ListenableFuture>. A ListenableFuture is returned whose value is a list of all of the results of the inputs. The values returned in the list are in the same order as the original list. If any of the input values were cancelled or failed then the returned ListenableFuture will be cancelled or fail as well. Canceling the returned future from the allAsList call will not propagate down to any of the original tasks submitted in the list.

ListenableFuture<List<Person>> lf1 = getPersonsByFirstNameFuture("martin");
ListenableFuture<List<Person>> lf2 = getPersonsByFirstNameFuture("bob");
ListenableFuture<List<List<Person>>> lfResults = Futures.allAsList(lf1, lf2);
//assume lf1 failed
List<List<Person>> personLists = lfResults.get() //call results in exception

SuccessfulAsList

The successfulAsList method is very similar to allAsList, but is more forgiving. Just like allAsList, successfulAsList returns a list of results in the same order as the input list, but if any of the inputs failed or were cancelled the corresponding value in the list is null. Canceling the returned future will not cancel any of the original inputs as well.

ListenableFuture<List<Person>> lf1 = getPersonsByFirstNameFuture("martin");
ListenableFuture<List<Person>> lf2 = getPersonsByFirstNameFuture("bob");
ListenableFuture<List<List<Person>>> lfResults = Futures.successfulAsList(lf1, lf2);
//assume lf1 failed
List<List<Person>> personLists = lfResults.get();
List<Person> listOne = personLists.get(0) //listOne is null
List<Person> listTwo = personLists.get(1) //listTwo, not null

Conclusion

Hopefully this was helpful in discovering the usefulness contained in the Futures class from Google Guava. I have created a unit test that shows example usages of the methods described in this post. As there is a fair amount of supporting code, I have created a project on gihub, guava-blog. This project will also contain the source code from my previous posts (Monitor, ListenableFuture) on Guava. As always comments and suggestions are welcomed.

Resources

  • Guava Project Home
  • Futures API
  • Source Code for blog series

 

From http://codingjunkie.net/google-guava-futures/

Google Guava Google (verb)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • When Should We Move to Microservices?
  • Reliability Is Slowing You Down
  • Demystifying the Infrastructure as Code Landscape
  • Create a CLI Chatbot With the ChatGPT API and Node.js

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • 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: