#88 Runnable vs Thread in Java

preview_player
Показать описание
Check out our courses:

Coupon: TELUSKO10 (10% Discount)

Coupon: TELUSKO20 (20% Discount)

For More Queries WhatsApp or Call on : +919008963671

Udemy Courses:

In this lecture we will learn:
- Using thread through Runnable interface
- How to start a thread with a Runnable interface
- Difference between extending a thread and implementing a runnable interface
- Use of anonymous class with runnable interface
- Creating a thread with lambda expression

#1
Multiple Inheritance is not supported by Java. So, extending a thread is not a good practice to follow.
- Thread is a class that implements Runnable and Runnable contains a method known as the run() method.
- Instead of extending a thread, we can also implement it through an interface called Runnable.
class A implements Runnable
{
public void run()
{
statements;
}
}

#2
- In the Runnabe method, the start() method is not present so we can not use it by implementing Runnable simply.
- Thread has multiple constructors and one of the constructors takes a runnable object.
- We cannot create an object of a thread by using a class name.
- Objects for a thread will be created by using a Runnable keyword. So, we create a reference of an interface and an object of a class
e.g., Runnable obj= new A();
- We have to pass a reference to an object in the thread class.
- After creating a reference of the Runnable class, we can use the start() method with the thread.

#3
- We can create a thread by using two methods:
1. Extend a thread class
2. Implement a Runnable interface
The runnable interface does not have thread methods, in that case, we need to create a separate thread object to use features.

#4
- We can also instantiate a runnable interface by using an anonymous class.
- Runnable is a functional interface so we also use lambda expression with it.

More Learning :

Donation:
PayPal Id : navinreddy20
Рекомендации по теме
Комментарии
Автор

really helped to understand why to make those threads .... thanks alot bruh!!!

eshanbhat
Автор

I understood it very well and applied it multiple times myself thank you for the beautiful explanation you are a great Teacher.
But I had one issue it never displayed Hi and Hello one after the other, it always displayed a few lines of hi then a few lines of hello and so on till the end of the loop.

Headcator
Автор

public class RunnableThread
{
public static void main(String[] args)
{
Thread t1 = new Thread( ()-> // passing the runnable object in form of lambda expression.
{
for(int i =1; i<=100; i++)
{

try {Thread.sleep(4);} catch (InterruptedException e) {e.printStackTrace();}
}
}
);
Thread t2 = new Thread( ()-> // passing the runnable object in form of lambda expression.
{
for(int i =1; i<=100; i++)
{
yadav");
{Thread.sleep(4);} catch (InterruptedException e) {e.printStackTrace();}
}
}
);
t1.start();
try {Thread.sleep(2);} catch (InterruptedException e) {e.printStackTrace();}
t2.start();

}
}
is it good way do this?

minchalaganesh
Автор

I am very new to Java, I have searched in google that interfaces can never be instantiated
"We can never instantiate an interface in java. We can, however, refer to an object that implements an interface by the type of the interface"
Then how below code can work?
Runnbale obj1=new Runnable();
It did work in my end too. But how? Runnable is an interface.

aninditadas
Автор

sir how you click to keyword and can see that keyword previous history in VS code. Which extension you use?

omorfarouk
Автор

sir i have a doubt
doubt: when we are using lambda expression why are we not including the run method inside it while assigning to the runnable object followed by thread

rockingstars
Автор

Sir im getting the output even if my obj1 and obj2 are the refernce of classes instead of interface

SrijithChetla
Автор

lamda expression is similar to arrow function some what

masteradvisor
Автор

Why are we giving
Runnable obj1 = new A();

Why not
A obj1 = new A();

We can use parent reference = new child object. But why not second case.

And can we give Runnable obj(parent reference) because Runnable is an interface not a class.

sakthipriya