Multithreading with java 8

This a continuation blog of the previous blog "Getting started with Multithreading in Java".

As we all know java 8 came with multiple features to make developer's life easier. In this article, we will see how can we use Java 8 feature in multithreading.

We all know there are two multiple ways to create a thread one is using the Runnable interface and another one is using the Thread class. Now for the Runnable interface, we can create an anonymous class to create a thread. Just like below.

Now as per Java 8, an interface having only one abstract method is a functional interface and see below that Runnable is a functional interface as it has only one abstract method i.e. run().

Now for a functional interface, we can use lambda expression to implement the abstract method, so the above code can be shortened like below if we use lambda expression.

Now that we have only one line of code for the run() method we can remove the curly brackets and the code will be shortened more.

In the above code, we are referencing the anonymous class object by a parent class reference variable 'r'. Now rather than using this variable, we can pass an anonymous object to the Thread constructor to create the thread like below and we can reduce the line of codes.

See how we have reduced the line of codes using Java 8. In one line only we are implementing the Runnable interface, creating an object of that class, passing that object as a parameter to the Thread class constructor and creating a thread.

I have used a few terms in this article which may be new to a few readers so let me explain.

Anonymous class:

a class that doesn't have a name is called an anonymous class. so if we want to implement an interface and we are sure that we are not going to use that implementing class further in that case rather than creating a new class in a different/same java file and implementing the interface we can implement the class in the same line where we create an object of the class. and as we don't have a name of that class we can not create 2nd object of that class. See the example below.

public class Demo {
    public static void main(String[] args) {
        DemoInterface di = new DemoInterface() {
            @Override
            public void test() {
                System.out.println("test method of Anonymous class implementing DemoInterface ");
            }
        };
        di.test();
    }
}
interface DemoInterface {
    public abstract void test();
}

/*
OUTPUT:
test method of Anonymous class implementing DemoInterface 
*/

Anonymous object:

An object which never had/has a reference variable is called an Anonymous object. this kind of object can be used once. See the example below.

public class Demo {
    public static void main(String[] args) {
        new Print().write(); // here 'new Print()' is an anonymous object
    }
}
class Print {
    public  void write(){
        System.out.println("I am writing");
    }
}

/*
OUTPUT:
I am writing
*/

Did you find this article valuable?

Support Java Blogs By Hemant by becoming a sponsor. Any amount is appreciated!