Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8943003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:40:04+00:00 2026-06-15T11:40:04+00:00

Our application has a number of services that implement ListenableFuture -based APIs, to the

  • 0

Our application has a number of services that implement ListenableFuture-based APIs, to the tune of:

public interface MyService {
    ListenableFuture<Thing> getMyThing();
    ListenableFuture<?> putMyThing(Thing thing);
}

As our domain model is not at all thread-safe, we run most of our code, except for the aforementioned services, on a well-known single-thread Executor. I think it would be nice if the services could guarantee that any listener added to the Futures they produce will be called on that Executor.

Of course, I could very well enforce this in the services’ clients, by calling ListenableFuture.addListener, Futures.addCallback or Futures.transform with the appropriate Executor argument but what I’m aiming at is precisely reducing the complexity and possibility of errors in the client code, so I would like the listener calls to happen on the well-known Executor when those methods are called without passing an Executor argument.

So, for now I’ve been implementing the services’ methods this way:

class MyServiceImpl {
    private Executor executor; /* the "main" executor */

    public ListenableFuture<Thing> getMyThing() {
        ListenableFuture<Thing> future = ...; /* actual service call */

        return Futures.transform(future, Functions.<Thing>identity(), executor );
    }
}

First, does this even work? From the Guava source, it seems that it does, but I would be glad for some kind of confirmation, and I’m having a bit of a hard time thinking about unit testing this.

Moreover, I’m a bit worried about the usefulness/cost ratio of the whole “Service calls back on specified thread (by default)” pattern. Does anyone have experience with something like this? Are there any hidden pitfalls in this approach?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T11:40:06+00:00Added an answer on June 15, 2026 at 11:40 am

    Your proposed identity() solution should work — with two exceptions.

    Proof: First, notice that the only reasonable way for Futures.transform to learn when its input is done is through a call to input.addListener(). We know that addListener() respects the given executor. (If you don’t believe me: ListenableFutureTask uses ExecutionList, which invokes listeners in only two places: add() and execute(). In both places, it uses the given executor.) Thus, any task run at the time that the input Future completes will run in the given executor.

    The key phrase here is “run at the time that the input Future completes.” The exceptions:

    • If someone adds a listener after the Future is complete, it will run in the thread that calls addListener.
    • If someone cancels the wrapper Future (as opposed to the original), the listeners will run in the thread that calls cancel.

    Depending on how your application is structured, these exceptions might be OK, but they’re something to keep in mind. If they are a problem, you may want to use a ForwardingListenableFuture solution (like eneveu’s but apparently less strict).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Our Java application has a number of modules which implement a common interface. By
Our application has a couple of shell scripts that are called from web-based Oracle
Our application has many controls that are created dynamically. For example, a navigation pane
I'm currently going through the process of running FxCop through our application that has
My company has a ClickOnce application that has been in use with our customers
I'm currently in the process of writing an application that has quite a number
Our web application has version numbers that get served out to the client on
We're developing an application that has to query 3D shapes (and query based on
Our application has a server-node that delegates work to numerous worker-nodes that are remote
Our application has a service layer and a DAO layer, written as Spring beans.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.