Neil Derno
Neil Derno

Reputation: 87

Returning a value from a Thread?

//... Some annoying getter 
ExecutorService es = Executors.newSingleThreadExecutor();
Future<Integer> result = es.submit(new Callable<Integer>() {
    public Integer call() throws Exception {
        //Get some value from the SQL database.
    }
});

return result;

Okay, I've looked all over. I need to know how I can make this wait until it finishes retrieving a value from a database to return this.

Upvotes: 2

Views: 99

Answers (1)

aioobe
aioobe

Reputation: 420990

You use result.get() to wait for the task to finish and to retrieve the result.

The API documentation is your friend. Here's the page describing the API of Future.

Upvotes: 6

Related Questions