Returning a value from a Thread?

本小妞迷上赌 提交于 2021-02-19 05:46:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!