//... 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;
好的,我已经全部看过了。我需要知道如何让它等待,直到它完成从数据库检索值以返回该值。
最佳答案
您使用result.get()
等待任务完成并检索结果。
API 文档是您的 friend 。这是描述 Future
的 API 的页面.
关于java - 从线程返回值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28106857/