6
Executors.newFixedThreadPool(3).submit(() -> {doSmth();});

"Ambiguous method call. Both submit (Callable) in ExecutorService and submit (Runnable) in ExecutorService match."

How to fix it? I know that I can use anonymous class but I'd like to use Function.

Eugene To
  • 1,890
  • 2
  • 19
  • 30
  • 1
    Works at my IDE (IntelliJ) w/o a problem. Furthermore, if `doSmth` is an instance method, you can rewrite this as `Executors.newFixedThreadPool(3).submit(this::doSmth);` – Smutje Nov 20 '14 at 07:27
  • 1
    What IDE/compiler? Are you sure your lambda has a block body? (`{doSmth();}` and not `doSmth()`.) Could be a bug. – Radiodef Nov 20 '14 at 07:56
  • Check whether this is your issue: http://stackoverflow.com/q/23430854/2711488 – Holger Nov 20 '14 at 09:29
  • Works on Eclipse too, no compilation errors. – ZhekaKozlov Nov 20 '14 at 11:30
  • I don't think this is the same as the linked duplicate, since that was a JDK bug, and this is a "resolve ambiguity" issue... – rogerdpack Nov 23 '16 at 18:26

2 Answers2

13
Executors.newFixedThreadPool(3).submit(() -> 1); //Callable
Executors.newFixedThreadPool(3).submit(() -> {doSmth();}); //Runnable
Sergey Morozov
  • 4,528
  • 3
  • 25
  • 39
3

In ambiguous cases explicitly provide the type:

Runnable runnable = () -> {doSmth();}
submit(runnable);
//or:
submit((Runnable) () -> {doSmth();});
McDowell
  • 107,573
  • 31
  • 204
  • 267
  • 4
    It shouldn't be necessary, though. The OP has a block body so for it to be a `Callable` it needs to explicitly return a value. – Radiodef Nov 20 '14 at 08:12