Hi everyone!
Welcome back to this tutorial.
In this tutorial we will implement the runnable interface using the legacy way, the way that is created
by using the anonymous classes and then the modern Java way that is using Lambda.
After that we will compare both and discuss the advantages of one over the other.
Let's go ahead and code this one time to set up our base project.
So in this case I have the IntelliJ open here.
At the time of recording the lecture.
The IntelliJ version that I'm using right now is 2024 .3.1.
So you need to have the latest version of IntelliJ in order for 21 Java version to be supported.
If you have IntelliJ versions like 2022 or something like that, or 2023 for that matter, because some
of the versions of 2023 still doesn't support Java 21.
So please ensure you have the latest version of IntelliJ.
The next step is I'm going to click on the new project.
So in here I'm going to give the project name as Java eight, okay?
Because for the first part of the course, we will be exploring all the Java eight features.
And Java eight is a significant release.
So I'm going to be creating the project name as Java eight.
And in here we have the whole Java eight project sitting under a project named Modern Java two.
In your case, you can give any folder you want.
And now the next step is we need to select the right version of Java.
So in this case I have selected the Java 21.
And then click on create over here.
So this is going to be creating the project in your machine.
As you can see the project is getting loaded.
So it is still indexing the configuration metadata.
Once it is completed then this project is set up and ready to use.
There you go.
We are going to create a package here.
So what are we going to implement as part of this project.
We are going to implement the runnable interface.
The first step in the process is that we are going to explore the lambdas, which got introduced as
part of JDK 1.8.
I am going to create a package called.
lambdas.
Good.
The next step in the process is that we are going to implement a runnable interface as part of this
tutorial.
So I'm going to create a class called.
Runnable example okay.
Otherwise I can name it as runnable Lambda example.
Runnable lambda example okay.
Before we go ahead and implement the runnable interface, let's talk about runnable interface first.
What is that is going to do and what is that.
What is a method that it has as part of this functional interface?
Most of the experienced developers would have worked on this interface at some point in their life.
But still, I'm going to give you a quick explanation about it.
First step in the process is that I'm going to open the runnable interface in IntelliJ.
I'm going to press the shift key twice.
It's going to give me this search everywhere option and then type in runnable.
Runnable.
If you take a look at it the second one here which stands for I here which is an interface I'm going
to select this one.
If you go and take a look at it, this runnable interface has only one method which is called run.
This method doesn't take any input, does not return any output.
So the idea of this interface is to work with the thread class.
And the next thing if you notice there is a new annotation called at Functional interface Annotation,
which is introduced as part of JDK 1.8.
This annotation is an optional annotation.
If you are going to create any functional interface of your own, it is not mandatory that you have
to provide this annotation, but this is an optional and annotation that helps to determine whether
that interface is a functional interface or not.
Okay.
Let's go ahead and implement this runnable interface.
First step in the process is that we are going to make this class an executable class.
If you type in PS VM in IntelliJ it's going to give you the public static void main.
That is the shortcut to generate this method.
The next step in the process is that I'm going to create the runnable interface.
Prior to Java eight I'm going to give some comments over there.
Prior Java eight.
First step is runnable runnable equal to.
We are basically creating an anonymous class.
That's the way we normally implement and functional interface prior to Java eight.
Runnable.
There we go.
I'm going to keep this really simple.
I'm just going to print something inside this one.
Inside runnable one.
Awesome.
Right now we have the instance of runnable ready.
The next step is we will pass this runnable to the thread and start the thread, which is going to run
the method whatever we have implemented here.
So the way to create the instance of thread is new thread and then pass the runnable interface.
Right.
So that's the next step.
Pass the runnable interface dot start as soon as I run this.
Basically this thread is going to start this runnable.
And then it is going to run the whatever implementation that we have inside this run method.
Could not find or load the main class.
This sometimes happens.
The way to fix this issue is that click on this build and then build module Java eight.
Or you can select build project can use either one of those options.
I'm going to run it again.
So we have the same problem?
And then let's do this.
Recompile.
Go implemented something.
Go ahead and run this.
There we go.
The program finally worked.
So here we have the inside runnable implementation.
Whatever we have implemented is called and it got printed in the console.
What is the next step.
The next step is that we know this right.
We have seen this over have worked on this runnable interface would have seen this implementation already.
There is nothing new at this point.
Let's go ahead and implement this using the Java eight Lambda syntax.
Java eight Lambda.
So here let's review the syntax again.
The concept is this is a input followed by that there is an arrow.
And open and close braces end with semicolon.
This is a syntax of lambda.
Let's go ahead and implement this using Lambdas.
So we are going to implement the runnable interface using Lambda.
I'm going to give a name Runnable Lambda.
And then what does this method take as an input.
Nothing right.
So no input.
And then the arrow followed by that we need the.
Curly braces.
In here we are going to write the implementation of it.
The implementation here is that I'm going to name it as inside runnable two to differentiate between
this implementation and that implementation.
Okay.
If you take a look at the syntax, the syntax is really really simple with the lambda implementation
of the same runnable interface.
It is concise.
It looks really simple.
Now let's go ahead and run this one new Read the runnable lambda dot start.
Okay, I'm going to run this program.
There you go.
We have both of those implementation working one with the Lambda way, the other with the legacy way.
What is the difference if you take a look at it, there is a lot of boilerplate code that is avoided
with the Lambda implementation.
And we can make this implementation of Lambda much simpler than whatever we have.
Go ahead and write that in a more simpler way.
So I'm going to have runnable I'm going to name it as Runnable Lambda one.
It doesn't take any input this.
So here this is a single line of statement right.
If you are having if you are writing any lambda expression that is just going to execute a single line
of statement, those kind of scenarios, you don't even need the curly braces.
This is enough.
So you have implemented the runnable interface in a single line.
We take a look at it.
If you compare this one with the prior Java wave, which is a legacy way of creating anonymous classes.
Compare the number of lines here.
The number of lines that we have is one, two, three, four, five, six.
Here just one single line and it is more readable compared to the legacy way.
Let's go ahead and run this.
New thread runnable Lambda one dot start.
Okay.
Let's run this.
It worked as expected, but if we take a look at it, we are creating this lambda expression and then
assigning that to a variable.
And then we are passing that variable to the thread instance.
Let's say you don't want to do that.
You don't want to create a runnable variable and assign a lambda expression to it.
You can do that like this too.
So new thread and then pass in the lambda expression directly as an parameter to the thread.
Code.
So double for and then start the thread.
I'm going to run this.
There you go.
It printed the value in the console.
If you have to do this in the legacy way, let's go ahead and take a look at it and implement that.
The new thread.
New runnable.
System dot out dot println inside runnable 3.1.
Look what the implementation and check the difference between these two here.
It looks really clumsy.
The code is not clear at all.
If some other developer have to look at the code, they have to break their head.
So with the Lambda, the implementation is really, really simple and it is easy.
It might be really tough in the initial stages of adapting to the lambda expressions, but once you
get into it, you will love it, I guarantee you that.
I would like to cover one more scenario before we wind up the runnable implementation using Lambda.
So here I mentioned for single statements you don't need the curly braces.
Let's say you have two statements, meaning you are going to print twice in your code.
In those kind of scenarios this is not valid.
You cannot give multiple statements without curly braces.
So for these kind of scenarios you need to have curly braces here now to make this statement a valid
one.
So if you want to make this valid, you have to go like this.
Then we will provide the semicolon here.
So you whenever you have multiple statements inside the lambda body then in those kind of scenarios
you will have to use curly braces.
Otherwise your compiler will complain.
Hey there is a syntax here error.
Please go ahead and fix it.
I'm going to run this and I have inside A33 twice.
You see, it got printed twice.
What I'm going to do.
Let's go ahead and do a quick summary of whatever we discussed.
So these are the certain things which you have to keep in your mind.
So if you are using Lambda and then you have just single statement or expression as part of the lambda
body, then curly braces are not needed.
If you are using multiple statements, meaning if your lambda body is gonna execute multiple statements,
then curly braces are mandatory.
Otherwise your compiler is going to complain about it.
I know we did this.
We discussed about it, but still, I want to stress this again and then give you a overview or a summary
of whatever we have done in the legacy way.
If you take a look at it, there is a lot of boilerplate code which is involved in order to create an
instance of a functional interface, but that is not the case with Lambda.
With Lambda, it is really, really simple and it is more readable.
Okay, so the next thing, no need of creating anonymous classes with lambda.
With lambda, it is evident that it lets you write more readable and concise code.
With this, we came to the end of this tutorial.
Let's discuss about the comparator interface in the next tutorial.
Thank you for watching.