猿问

在Java中使用异步后,主方法如何返回异步中得到的值?

一个返回值为User的方法,在内部调用了异步方法(比如Rxjava,或者异步的网络请求),其内部匿名函数内才能拿到user对象,那么我的方法应该怎么return这个对象?

吃鸡游戏
浏览 5300回答 7
7回答

白板的微信

方法的返回值为user,那这个方法就不能叫异步方法了。除非这个方法返回future,或者类似于可以在之后拿到结果的引用,这个方法才能叫异步方法。想调完方法后得到user,那么方法里面就没有必要再放置异步的代码了,一点意义都没有。异步的返回结果只能通过回调。同步的方法通常是这样的public User syncGetUser() {&nbsp; &nbsp; User user = heavyWork();&nbsp; &nbsp; return user;}由于heavyWork方法可能需要查数据库,或者做大量计算,所以heavyWork方法会执行大量的时间。如果你不想等待大量的时间,这时候异步就可以派上用场了。public Future<User> asyncGetUser() {&nbsp; &nbsp; Future<User> future = threadPool.submit(new Callable<User> {&nbsp; &nbsp; &nbsp; &nbsp; public User call() throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return heavyWork();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; return future;}到了这里,heavyWork已经交给另外一个线程去跑了,并返回一个future给你。之后,你可以通过这个future的get方法得到你想要的user。这才是异步的意义和用处所在。题主的题目本身就是矛盾的。在一个含有异步代码的方法里面返回异步执行的结果,是个矛盾的事情。

有只小跳蛙

我没有研究过 RxJava,不过之前团队里面做 Android 的开发的同事说是很好用。1 楼给了一种使用 Future 的方案,但是很抱歉 Future 是一种异步阻塞式的API,即没有通知回调。至于回调怎么实现,这个应该很基本,去看看观察者模式就好了,本质还是利用接口的多态特性。不过可以使用 google guava,里面提供了一种增强的 Future 为 ListenableFuture。这里引用 1 楼的例子,进行修改。&nbsp;class A implements FutureCallback{&nbsp; &nbsp; &nbsp; &nbsp; ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10));&nbsp; &nbsp; &nbsp; &nbsp; ListenableFuture futrue = service.submit(new Callable() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public User call() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return heavyWork();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; Futures.addCallback(furtrue,this);&nbsp; &nbsp; &nbsp; &nbsp; public void onSuccess(User user) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //这个是回调执行的代码&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public void onFailure(Throwable thrown) {&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }

aluckdog

异步方法的“返回”在执行代码之前。代码都还没执行,怎么返回结果?

冉冉说

我觉得使用回调方式也可以

米脂

使用CountDownLatch 可以将异步操作,转换为同步操作。final CountDownLatch latch = new CountDownLatch(1);
调用异步方法。在异步回调结果中,latch.countDown();
然后try {
latch.await();
} catch (InterruptedException e) {
&nbsp;}

return 数据;

精慕HU

java里有线程返回Callable接口
随时随地看视频慕课网APP

相关分类

Java
我要回答