问题
//... 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.
回答1:
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.
来源:https://stackoverflow.com/questions/28106857/returning-a-value-from-a-thread