187

Is there a standard functional interface in the JDK that takes nothing and returns nothing? I cannot find one. Something like the following:

@FunctionalInterface
interface Action {
  void execute();
}
deamon
  • 89,107
  • 111
  • 320
  • 448
  • 3
    Already answered and accepted, but this is a duplicate of http://stackoverflow.com/q/23868733/1441122 . That other question is kind of hard to find, though. – Stuart Marks May 30 '14 at 17:44
  • 1
    Indeed - I edited that other question to make it more generic (i.e., basing the question on a a C# method). – BeeOnRope Jun 07 '16 at 01:23
  • 1
    I disagree with the use `Runnable` advice. It miscommunicates intent: https://stackoverflow.com/a/76741622/501113 – chaotic3quilibrium Jul 21 '23 at 22:52

1 Answers1

278

How about Runnable :

@FunctionalInterface
public interface Runnable {
    /**
     * When an object implementing interface <code>Runnable</code> is used
     * to create a thread, starting the thread causes the object's
     * <code>run</code> method to be called in that separately executing
     * thread.
     * <p>
     * The general contract of the method <code>run</code> is that it may
     * take any action whatsoever.
     *
     * @see     java.lang.Thread#run()
     */
    public abstract void run();
}
Eran
  • 387,369
  • 54
  • 702
  • 768
  • 15
    Yes, that's it! I've forgotten the good old interfaces and looked only in [java.util.function](http://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html). – deamon May 30 '14 at 16:12
  • 9
    Huh, `public abstract` in an interface? Tisk tisk, JDK people! (Never noticed that before.) – yshavit May 30 '14 at 16:18
  • 2
    I think it used to be [mandatory once upon a time](http://stackoverflow.com/questions/7202616/java-abstract-interface). – aioobe Nov 13 '14 at 09:43
  • 36
    FWIW... while it satisfies the functional requirement, it does not sound very semantic, Runnable is commonly associated with creating threads. A simple Worker funcional interface with a doWork method would have been nice. EDIT: Oops: http://stackoverflow.com/questions/27973294/function-with-no-args-and-no-return-type-void-in-java-8?lq=1#comment44339647_27973294 – jpangamarca Aug 19 '15 at 15:46
  • 15
    I used `Runnable` in this way, and then someone saw this Runnable, and thought "runnable, aha, threads" and edited the code and forked a thread. That resulted in a bug. So nowadays I don't use `Runnable` as a callback. Related question (well, answer): http://stackoverflow.com/a/30183786/694469 – KajMagnus Dec 10 '15 at 09:45
  • I think this interface will confuse developer, they will think that the function will do something with Threads – Adelin Oct 11 '17 at 13:13
  • 24
    Quoting the [javadoc](https://docs.oracle.com/javase/8/docs/api/java/lang/Runnable.html): `The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread.` I wouldn't recommend using `Runnable` just for the purpose of no parameter, void-returning functional interface. – AndrewMcCoist Jan 02 '18 at 13:07
  • 1
    @AndrewMcCoist That quote does not mean that the `Runnable` interface cannot have other uses that do not involve Threads. – Eran Jan 02 '18 at 13:13
  • @Eran Of course, it doesn't. The thing is, many, many developers noticing a `Runnable` might think that this involves some threading logic and find the code confusing. – AndrewMcCoist Jan 11 '18 at 10:25
  • 1
    @AndrewMcCoist Well, `Runnable` is the only functional interface in the JDK having a method with no parameters and void return type. You can either use it whenever you need such interface, or you can define your own interface for cases you think the `Runnable` interface is not appropriate for. – Eran Jan 11 '18 at 10:52
  • 1
    @Eran I totally agree, it's the only "suitable" functional interface in the JDK. The thing is, it's been used for years in situations, when an application required some custom threading logic. Thus usage of Runnable in non-threading specific scopes may be confusing. That's I would rather create a custom functional interface. – AndrewMcCoist Jan 15 '18 at 11:04
  • 1
    If you don't like the semantic of Runnable you can always use `Callable`. – Andrea Bergonzo May 27 '19 at 13:32
  • @yshavit, all java interfaces have public abstract methods, it is been there from birth of java and into Java 8 : all functional interfaces have single abstract method – Ram Kowsu Oct 01 '20 at 23:35
  • @RamKowsu Yeah, I was just referring to the fact that this this interface, the `public abstract` is actually written out. It usually isn't, since it's implied in interfaces. It was meant as a lighthearted comment, not anything to be taken very seriously. :-) – yshavit Oct 03 '20 at 19:58
  • 2
    Optional.ifPresentOrElse introduced in java9 takes a Runnable as the second parameter without any concurrency semantics, so the official view seems to be that there's nothing wrong with using Runnable in this way – avmohan Oct 28 '21 at 08:39