I have an underlying asynchronous out-of-order query/response system that I want to make synchronous. Queries and responses can be mapped by marking the queries with unique IDs that in turn will accompany the corresponding responses.
My attempt at making it synchronous uses two ConcurrentHashMap
s: one that maps from IDs to results, and one that maps from the same IDs to CountDownLatch
es. The code goes like this when executing a query:
public Result execute(Query query) throws InterruptedException {
int id = atomicInteger.incrementAndGet();
CountDownLatch latch = new CountDownLatch(1);
latchMap.put(id, latch);
query.executeAsyncWithId(id); // Probably returns before result is ready
try {
latch.await(); // Blocks until result is ready
return resultMap.remove(id);
} catch (InterruptedException e) {
latchMap.remove(id); // We are not waiting anymore
throw e;
}
}
And the code for handling an incoming result:
public void handleResult(Result result) {
int id = result.getId();
CountDownLatch latch = latchMap.remove(id);
if (latch == null) {
return; // Nobody wants result
}
resultMap.put(id, result);
latch.countDown();
}
This method is called from a thread that reads all the incoming results from the underlying system (there is only one such reader thread).
First of all I am uncertain about the thread-safety, but it also seems unnecessary to use two HashMap
s for this (particularly because IDs are never reused). Any ideas for improvements?