问题描述
我有一个自定义对象,我需要在lambda线程内进行修改,因为我需要执行操作并为其分配一些值。
问题在于,当我在Thread()中声明变量时,无法从封闭函数中返回它。然后,如果我尝试使其成为全局变量并在Thread中为其分配一些值,则无法完成,因为lambda只允许在其内部使用final或有效的final变量。
对此有什么解决方法/解决方案?
// Gives an undesired result
public class MeClass {
public static Response response = new Response();
// TODO: Make response specific to a method and not global
public Response get(String endpoint) {
new Thread(() -> {
try {
this.response = OffredUtil.makeGetRequest(endpoint);
} catch (Exception e) {
this.response.isException = true;
Log.d(TAG,e.getMessage());
}
}).start();
return this.response;
}
// Another method with similar function accessing response
}
所以我想在方法本身内部声明response
,但是由于只有最终变量可用,所以我不能这样做。
// Gives an error
public Response get(String endpoint) {
Response response = new Response();
new Thread(() -> {
try {
response = OffredUtil.makeGetRequest(endpoint);
} catch (Exception e) {
this.response.isException = true;
Log.d(TAG,e.getMessage());
}
}).start();
return response;
解决方法
假设允许这样做?您希望它返回什么?
// Warning! This is an example of what *NOT* to do.
//
public Response get(String endpoint) {
Response response = new Response();
new Thread(() -> {
response = OffredUtil.makeGetRequest(endpoint);
}).start();
return response;
}
没有理由认为response = OffredUtil.makeGetRequest(endpoint);
语句将在return response;
语句之前执行。实际上,它可能不会直到一段时间后才会执行。
您真正想要的是;
- 让您的
get(endpoint)
方法返回一个 mutable 对象,并且 - 一种调用方等待 的方法,直到其他线程将新值存储到可变对象中为止。
Java标准库仅为这种可变对象定义了一个接口:它称为java.util.concurrent.Future
。 Future
具有get()
方法,该方法将在必要时等待,直到其他线程通过为其赋值来完成 后,再get()
将返回该值。
最简单的使用方法是通过CompletableFuture
类:
import java.util.concurrent.Future;
import java.util.concurrent.CompletableFuture;
...
public Future<Response> get(String endpoint) {
return CompletableFuture.supplyAsync(() -> {
return OffredUtil.makeGetRequest(endpoint);
});
}
对此get(endpoint)
方法的调用会将任务提交给内置线程池,该线程池将执行给定的lambda表达式,然后它将返回一个Future
,该任务将完成该任务
如果lambda产生一个值,那么它将成为Future
的值。如果lambda引发异常,则将捕获该异常,并且该异常对象将存储在Future
get(endpoint)
的呼叫者可以执行以下操作:
...
Future<Response> fr = myClassInstance.get(endpoint);
doSomethingElseConcurrentlyWithThe_makeGetRequest_call(...);
try {
Response r = fr.get();
...
} catch (Exception e) {
o.response.isException = true;
Log.d(TAG,e.getMessage());
}