As I understand it, you want to know why you seem to be able to pass a "Function" to the ThreadPoolExecutor.submit()
method that takes a Callable
, not a Function
.
The explanation is that the method can't take a Function
as a parameter; what you're seeing is a Callable
being passed in, expressed as a lambda
expression (Essentially an expression that uses the right arrow (->
) syntax).
As outlined in the previous document, a lambda expression is used to provide an implementation of an interface that has exactly one abstract method that is not an override of a method on Object
. These are typically annotated FunctionalInterface
, which is possibly where your confusion arose, although this is not a requirement as outlined in the Language Specification:
A functional interface is an interface that has just one abstract
method (aside from the methods of Object
), and thus represents a single
function contract. This "single" method may take the form of multiple
abstract methods with override-equivalent signatures inherited from
superinterfaces; in this case, the inherited methods logically represent
a single method.
In your example above, depending on the return type of myManager.createAccount()
, you're either providing an implementation of Callable
(if you return anything), or Runnable
if it's a void
method:
() -> myManager.createAccount(token, profile)
Your lambda matches the method signature of the abstract call()
or run()
methods because it takes no parameters (As indicated by the ()
on the left hand side of the arrow), and returns void
or some result.