谁能举例说明如何在 java 中的 InvokeAll() Callable 方法中使用 Generic?

Can anyone give example of how to use Generic in InvokeAll() Callable method in java?

我有两个单独的 taskLists 并且想要使用 ExecutorService 中的 invokeAll() 来执行这两个任务列表的通用/通用方法。我能够获得每个 TaskList 的响应和 invokeAll() 。 但是无法写一个common来执行并得到结果。

定位用户任务:

List<LocateUser> taskList = new ArrayList<>();
taskList.add(new BSLocateUserClient(url, locateName, username, token));
List<Future<LocateUserResponse>> locateResponse = executor.invokeAll(taskList);
locateResponse.forEach(response -> {
    LocateUserResponse user;
    try {
        user = response.get();
    } catch (InterruptedException | ExecutionException e) {
        //
    } finally {
        executor.shutdown();
    }
});

登录响应任务:

List<LoginUser> taskList = new ArrayList<>();
for (String url : urls) {
    taskList.add(new BSWebserviceClient(url, username, password, isOciLogin22, ociWebServiceTemplateFactory));
}

List<Future<LoginResponse>> response = executor.invokeAll(taskList);
List<LoginResponse> loginResponses = new ArrayList<>();
response.forEach(loginResponse -> {
    try {
        LoginResponse loginDetails = loginResponse.get();
    } catch (InterruptedException | ExecutionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        executor.shutdown();
    }
});

你能帮我解决这个问题吗?

我实现了这个 - 通过添加一个接口 ResponseTask 并将该接口实现到两个响应 class 并为 invokeAll 和 getResponse 创建一个通用/通用方法。

public interface ResponseTask extends Serializable {
} 

全部调用 :

public <T extends ResponseTask> List<T> invokeAll(Set<Callable<ResponseTask>> callables, int threadCount) {
        ThreadFactory customThreadfactory = new CustomThreadFactoryBuilder().setNamePrefix("MultiThreadExecutor-Calls")
                        .setDaemon(false).build();
        ExecutorService executor = Executors.newFixedThreadPool(threadCount, customThreadfactory);
        try {
            List<Future<ResponseTask>> threadResponse = executor.invokeAll(callables);
            return getResponse(threadResponse);
        } catch (InterruptedException e) {
            // Restore interrupted state...
            Thread.currentThread().interrupt();
        } finally {
            executor.shutdown();
        }
        return Collections.emptyList();
    }

获取响应

public <T extends ResponseTask> List<T> getResponse(List<Future<ResponseTask>> threadResponse) {
        List<BSLocateUserResponse> locateResponse = new ArrayList<>();
        List<LoginUser> userResponse = new ArrayList<>();
        for (Future<ResponseTask> response : threadResponse) {
            ResponseTask result = null;
            try {
                result = response.get();
            } catch (ExecutionException e) {
                // Todo Need to capture the specific exception to ignore here
                LOG.info("Exception : {}  occurred when calling multithread ", e.getMessage());
                continue;
            } catch (InterruptedException e) {
                // Restore interrupted state...
                Thread.currentThread().interrupt();
            }
            if (result instanceof BSLocateUserResponse) {
                locateResponse.add((BSLocateUserResponse) result);
            } else if (result instanceof LoginUser) {
                userResponse.add((LoginUser) result);
            }
        }
        if (locateResponse.isEmpty()) {
            return (List<T>) userResponse;
        }
        return (List<T>) locateResponse;
    }