Interface Callable

Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface Callable
Interface for executing code that does not return a value. Similar to Runnable, but not used to program threads. A Callable can be constructed inside an object's methods so that otherwise hidden fields and methods are accessible. For example:

  Map<String, Callable> table = ...;
  void foo() {
     final int i = 10;
     Callable callable = new Callable() {
         public void call() {
           System.out.println("i = " +i);
         }
       };
     table.put("foo", callable);
 }

 void bar() {
     table.get("foo").call();
  }
 

Applications can be coded so that the Runnable interface is used when code encapsulated by a Runnable is to be run in its own thread and a Callable can be used when the code that was encapsulated is executed in the caller's thread.

  • Method Summary

    Modifier and Type
    Method
    Description
    void
    The method to call.
  • Method Details

    • call

      void call()
      The method to call.