Wrapping a series of asynchronous calls with a synchronous method with a return value

asynchronous, concurrency, java, synchronous

Solution

If you want to turn an asynchronous operation (which executes a callback when finished), into a synchronous/blocking one, you can use a blocking queue. You can wrap this up in a Future object if you wish.

Define a blocking queue which can hold just one element:

`BlockingQueue<Result> blockingQueue = new ArrayBlockingQueue<Result>(1);`

Start your asynchronous process (will run in the background), and write the callback such that when it's done, it adds its result to the blocking queue.

In your foreground/application thread, have it take() from the queue, which blocks until an element becomes available:

`Result result = blockingQueue.take();`

I wrote something similar before (foreground thread needs to block for an asynchronous response from a remote machine) using something like a Future, you can find example code here.

Problem

My current code uses series of asynchronous processes that culminate in results. I need to wrap each of these in such a way that each is accessed by a synchronous method with the result as a return value. I want to use executor services to do this, so as to allow many of these to happen at the same time. I have the feeling that Future might be pertinent to my implementation, but I can't figure out a good way to make this happen. What I have now: ``` public class DoAJob { ResultObject result; public void stepOne() { // Passes self in for a callback otherComponent.doStepOne(this); } // Called back by otherComponent once it has completed doStepOne public void stepTwo(IntermediateData d) { otherComponent.doStepTwo(this, d); } // Called back by otherComponent once it has completed doStepTwo public void stepThree(ResultObject resultFromOtherComponent) { result = resultFromOtherComponent; //Done with process } } ``` This has worked pretty well internally, but now I need to map my process into a synchronous method with a return value like: ``` public ResultObject getResult(){ // ??? What goes here ??? } ``` Does anyone have a good idea about how to implement this elegantly?

Original source

Related problems