簡體   English   中英

使用Google番石榴時無法推斷類型變量V

[英]cannot infer type-variable(s) V when using 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)

我檢查了此代碼,發現沒有什么可以改善以解決此問題的? 番石榴的版本是: 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);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM