Implementing ListeningExecutorService in Java 5

删除回忆录丶 提交于 2020-01-04 13:47:52

问题


In Java 5, the ExecutorService interface declares the method:

<T> List<Future<T>> invokeAll(Collection<Callable<T>> tasks)
    throws InterruptedException;

whereas Guava 11.0.2, written in Java 6 but supposedly compatible with Java 5, overrides it in ListeningExecutorService as:

 <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
     throws InterruptedException;

If I want to implement my own ListeningExecutorService, I would need to implement both of these methods, but I am also not able to have two methods the same erasure, so it's a bit of a Catch 22.

Is there any way around this problem? More specifically, is there any way to implement a ListeningExecutorService in Java 5?

As a side note to any Guava folks--is it actually necessary for Guava to re-declare this method since it's already inherited from ExecutorService?


回答1:


The way that we made this work was to override the JDK's ExecutorService interface in our bootclasspath. You could do something similar during your project's compilation. The easiest way to see our setup is probably the change that removed it for release 12 (since that release will require JDK6).




回答2:


The original method signature has been reported as a bug and fixed for JDK 6: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6267833

To quote the resolution message:

  • is binary compatible.
  • is source compatible for users of an ExecutorService
  • requires minor source code changes for the small set of developers who have implemented ExecutorService without inheriting the default implementations in AbstractExecutorService. The set of affected developers are developers creating sophisticated thread pool applications, putting them into the "concurrency rocket scientist" category. They will generally appreciate this change. The possible compiler error is trivial to fix in the source code.



回答3:


The only way I can think of implementing both interfaces, scary as it is, is

List invokeAll(Collection tasks)

drop the generic types, document why you are doing it, and be very careful.



来源:https://stackoverflow.com/questions/10237259/implementing-listeningexecutorservice-in-java-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!