MetaChrome
MetaChrome

Reputation: 3220

In Java, should each query be in a separate thread?

If an application is blocking until a query returns a response, doesn't it make sense to execute queries on a thread pool?

Upvotes: 1

Views: 233

Answers (1)

Johan Sjöberg
Johan Sjöberg

Reputation: 49187

Only if you expect to perform work during that period, otherwise there's no sense doing it. A nice way to do it is to have your API return a Future. Something like:

interface QueryService {
   public FutureTask<QueryResult> query(Query q);
}

FutureTask<QueryResult> res = query(..);
// do work
res.get(); // blocks until result is in

Of course, you should typically submit your Callables or Runnables to an ExecutorService .

Upvotes: 5

Related Questions