Search code examples
javalambdajava-8functional-interface

Disambiguate functional interface in lambda


Suppose this:

ExecutorService service = ...;

// somewhere in the code the executorService is used this way:
service.submit(() -> { ... });

The lambda expression would default to Callable.
Is there a way to make it instantiate a Runnable instead?

Thanks for your help.


Solution

  • You can declare it as a Runnable, or use a cast:

    Runnable r = () -> { ... };
    executorService.submit(r);
    

    or

    executorService.submit((Runnable) () -> { ... });