Implementing Runnable - Java Lambda Stream

Java examples for Lambda Stream:Lambda

Introduction

Use a lambda expression to implement the java.util.Runnable interface.

The java.util.Runnable interface has only a single abstract method, run().

Demo Code

public class Main {

    public static void main(String[] args) {

        Runnable oldRunnable = new Runnable() {
            @Override/*from   w  w  w .  j a  v  a 2 s  .c  o m*/
            public void run() {
                int x = 5 * 3;
                System.out.println("The variable using the old way equals: " + x);
            }
        };

        Runnable lambdaRunnable = () -> {
            int x = 5 * 3;
            System.out.println("The variable using the lambda equals: " + x);
        };

        oldRunnable.run();
        lambdaRunnable.run();
    }
}

Result


Related Tutorials