java8 functional interface to handle the callback

There is also the possibility to use Consumer<Void>. It could look like this

public void init(Consumer<Void> callback) {
    callback.accept(null);
}

init((Void) -> done());

Package java.util.function does not contain a functional interface with a method that does not require any parameter and returns void. But you can use Runnable interface instead.

private void myGenericMethod(Runnable runnable){
    common task1;
    common task2;
    //consider checking if runnable != null to avoid NPE
    runnable.run();
}  

Then the invocation would look pretty simple:

myGenericMethod(() -> {
    //do something fancy
    System.out.println("Hello, world!");
});

Other options

There are other functional interfaces you may be interested in, for example:

  • Supplier<T> if you want to return a value of T without passing any parameters
  • Function<T,R> if you want to pass a value of T and return a value of R
  • Consumer<T> if you want to pass value T as a parameter and return void

Why there is no alternative for Runnable interface?

Using Runnable interface for a lambda that does not return nothing and does not expect any parameters may sound controversial for many programmers. Runnable was invented for running a code in a separate thread and many programmers identify this class with multithreading. Even documentation says:

The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread.

Someone already asked similar question 2 years ago and if you take a look at this comment and Brian Goetz's reaction to it you will understand that Java language designers came to a conclusion that there is no need to create another functional interface that mimics implementation of Runnable interface.