从线程返回值?

Nei*_*rno 2 java multithreading return

//... 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;
Run Code Online (Sandbox Code Playgroud)

好的,我看了一遍.我需要知道如何让它等待,直到它完成从数据库中检索一个值来返回它.

aio*_*obe 6

result.get()用来等待任务完成并检索结果.

API文档是您的朋友.这是描述API的页面Future.