从线程返回值? [英] Returning a value from a Thread?

查看:65
本文介绍了从线程返回值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

//... 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.

推荐答案

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

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

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

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

这篇关于从线程返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆