A relatively recent published article about replacing executors by actors in Java 8 stated that using an anonymous inner class Runnable like so:
// Functional operation
executor.execute(new Runnable() {
System.out.println(data);
});
Can be ideally replaced with a more garbage-collector friendly technique performing the same operation:
Actor<String> actor = new Actor<>(parentExecutor, ::onMessage);
// Equivalent functional operation
actor.act("Hello world");
public void onMessage(String message) {
System.out.println(message);
}
It makes sense that one should generally use method references over anonymous inner classes (i.e. new Runnable, new Callable). That idea has been around for a while. There seems to be another subtlety here using this Actor pattern that's more efficient, but not explicitly explained in the article.
I understand that using:
executor.execute(() -> System.out.println(data));
Will be apparently inefficient (albeit, slightly) because the compiler has to create a generated method invokedynamic "call site" and reference it when invoked.
Is the advantage here that (1) a direct method reference doesn't have the overhead of the invokedynamic method generation and (2) can additionally allows you to pass method parameters to this direct reference? What is the key takeaway of this idea, presented in the article, over the patterns we already use and know?