gpt4 book ai didi

java - 使用 google guava 时无法推断类型变量 V

转载 作者:行者123 更新时间:2023-12-02 02:00:18 26 4
gpt4 key购买 nike

我使用 google guava(Java 8) 为异步任务编写此代码:

public class AppStarter {
final static ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool());

public static void main(String[] args) {

ListenableFuture<Boolean> booleanTask = service.submit(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return true;
}
});

Futures.addCallback(booleanTask, new FutureCallback<Boolean>() {
@Override
public void onSuccess(Boolean result) {
System.err.println("BooleanTask: " + result);
}

@Override
public void onFailure(Throwable t) {
}
});
}

}

当我启动该函数时,抛出此错误:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0:compile (default-compile) on project soa-wallet-service: Compilation failure: Compilation failure: 
[ERROR] /var/jenkins_home/workspace/soa-wallet/soa-wallet-service/src/main/java/com/sportswin/soa/report/common/AsyncExecutor.java:[46,16] method addCallback in class com.google.common.util.concurrent.Futures cannot be applied to given types;
[ERROR] required: com.google.common.util.concurrent.ListenableFuture<V>,com.google.common.util.concurrent.FutureCallback<? super V>,java.util.concurrent.Executor
[ERROR] found: com.google.common.util.concurrent.ListenableFuture<java.lang.String>,<anonymous com.google.common.util.concurrent.FutureCallback<java.lang.String>>
[ERROR] reason: cannot infer type-variable(s) V
[ERROR] (actual and formal argument lists differ in length)
[ERROR] /var/jenkins_home/workspace/soa-wallet/soa-wallet-service/src/main/java/com/sportswin/soa/report/service/impl/WalletRecordService.java:[244,16] method addCallback in class com.google.common.util.concurrent.Futures cannot be applied to given types;
[ERROR] required: com.google.common.util.concurrent.ListenableFuture<V>,com.google.common.util.concurrent.FutureCallback<? super V>,java.util.concurrent.Executor
[ERROR] found: com.google.common.util.concurrent.ListenableFuture<java.lang.Integer>,<anonymous com.google.common.util.concurrent.FutureCallback<java.lang.Integer>>
[ERROR] reason: cannot infer type-variable(s) V
[ERROR] (actual and formal argument lists differ in length)

我检查了这段代码,发现没有什么可以改进来解决这个问题? Guava 版本是:28.0-jre。这是我的pom:

 <dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>28.0-jre</version>
</dependency>

最佳答案

添加代码的执行器参数:

@SpringBootApplication
public class WsClientApplication {

final static ExecutorService executors = Executors.newCachedThreadPool();

final static ListeningExecutorService service = MoreExecutors.listeningDecorator(executors);


public static void main(String[] args) throws InterruptedException {


ListenableFuture<Boolean> booleanTask = service.submit(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return true;
}
});

Futures.addCallback(booleanTask, new FutureCallback<Boolean>() {
@Override
public void onSuccess(Boolean result) {
System.err.println("BooleanTask: " + result);
}

@Override
public void onFailure(Throwable t) {
}
}, executors);
}
}

关于java - 使用 google guava 时无法推断类型变量 V,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57377684/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com