#88 Runnable vs Thread in Java

  Рет қаралды 106,787

Telusko

Telusko

Күн бұрын

Check out our courses:
Java Full Stack and Spring AI - go.telusko.com...
Coupon: TELUSKO10 (10% Discount)
DevOps with AWS: From Basics to Mastery : go.telusko.com...
Coupon: TELUSKO10 (10% Discount)
Master Java Spring Development : go.telusko.com...
Coupon: TELUSKO20 (20% Discount)
For More Queries WhatsApp or Call on : +919008963671
Udemy Courses:
Spring: go.telusko.com...
Java:- go.telusko.com...
Java Spring:- go.telusko.com...
Java For Programmers:- go.telusko.com...
Python : go.telusko.com...
Git : go.telusko.com...
Docker : go.telusko.com...
website : courses.telusk...
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.
Github repo : github.com/nav...
Java:- bit.ly/JavaUde...
Spring:- bit.ly/SpringU...
More Learning :
Java :- bit.ly/3x6rr0N
Python :- bit.ly/3GRc7JX
Django :- bit.ly/3MmoJK6
JavaScript :- bit.ly/3tiAlHo
Node JS :- bit.ly/3GT4liq
Rest Api :-bit.ly/3MjhZwt
Servlet :- bit.ly/3Q7eA7k
Spring Framework :- bit.ly/3xi7buh
Design Patterns in Java :- bit.ly/3MocXiq
Docker :- bit.ly/3xjWzLA
Blockchain Tutorial :- bit.ly/3NSbOkc
Corda Tutorial:- bit.ly/3thbUKa
Hyperledger Fabric :- bit.ly/38RZCRB
NoSQL Tutorial :- bit.ly/3aJpRuc
Mysql Tutorial :- bit.ly/3thpr4L
Data Structures using Java :- bit.ly/3MuJa7S
Git Tutorial :- bit.ly/3NXyCPu
Donation:
PayPal Id : navinreddy20
www.telusko.com

Пікірлер: 28
@eshanbhat4741
@eshanbhat4741 2 жыл бұрын
really helped to understand why to make those threads .... thanks alot bruh!!!
@minchalaganesh89
@minchalaganesh89 Жыл бұрын
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 // passing the runnable object in form of lambda expression. { for(int i =1; i
@Headcator
@Headcator Жыл бұрын
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.
@sakthipriya8653
@sakthipriya8653 Жыл бұрын
Try giving i
@shubhamsuryawanshi2206
@shubhamsuryawanshi2206 Жыл бұрын
its working @@sakthipriya8653
@likevideo00
@likevideo00 7 ай бұрын
Thanks a lot Navin
@likevideo00
@likevideo00 5 ай бұрын
Great, thanks a lot Navinji
@vamshimeta1202
@vamshimeta1202 5 ай бұрын
garu bol
@omorfarouk5262
@omorfarouk5262 Жыл бұрын
sir how you click to keyword and can see that keyword previous history in VS code. Which extension you use?
@rockingstars4121
@rockingstars4121 Жыл бұрын
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
@ritikdwivedi5983
@ritikdwivedi5983 Жыл бұрын
Lambda expression is used for functional interface, that means an interface having just one method. If you see Runnable interface it has just one method run. So the idea behind the lambda expression is to declare that run method with syntax sugar because there will always be one method and in the lambda expression we will adding it's definition. Hope this help
@aninditadas7348
@aninditadas7348 Жыл бұрын
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.
@HeadSHOOTdewunzz
@HeadSHOOTdewunzz Жыл бұрын
Take a look at 6.15! We are not instantiating directly the interface, but with the help of the anonymous class we are creating a class to implement that interface, and therefore we can create an object. In the code in the brackets right after "new Runnable()", we are basically implementing the interface without creating an actual separate class, but we are doing it in the same file and inside an already existing class: that is the concept of "Anonymous inner class"
@masteradvisor594
@masteradvisor594 Жыл бұрын
lamda expression is similar to arrow function some what
@sakthipriya8653
@sakthipriya8653 Жыл бұрын
Lambda expression and arrow function are the same, I guess.
@masteradvisor594
@masteradvisor594 Жыл бұрын
@@sakthipriya8653 i think there is much depth and uses of lamda than Arrow function but i think exploration is needed
@SrijithChetla10
@SrijithChetla10 5 ай бұрын
Sir im getting the output even if my obj1 and obj2 are the refernce of classes instead of interface
@jayanthreddy8459
@jayanthreddy8459 3 ай бұрын
yes bro because class implements interface
@adi1140
@adi1140 6 ай бұрын
You missed callable
@rishitathawani7200
@rishitathawani7200 2 ай бұрын
What?
@sakthipriya8653
@sakthipriya8653 Жыл бұрын
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.
@devd_rx
@devd_rx Жыл бұрын
its because we need Runnable type of object to pass to the thread object, so it can run the start method
@bipinkori4060
@bipinkori4060 4 ай бұрын
​@@devd_rxgreat! Your basic are very much clear 😅
@jayanthreddy8459
@jayanthreddy8459 3 ай бұрын
yes we can give A obj1 = new A(); as child inherits from parent. we can create reference of an interface and object of a class
@funnyadda374
@funnyadda374 4 ай бұрын
Mere isme to run ho gya
#89 Race Condition in Java
12:30
Telusko
Рет қаралды 88 М.
#95 Comparator vs Comparable in Java
15:43
Telusko
Рет қаралды 232 М.
Quilt Challenge, No Skills, Just Luck#Funnyfamily #Partygames #Funny
00:32
Family Games Media
Рет қаралды 55 МЛН
Каха и дочка
00:28
К-Media
Рет қаралды 3,4 МЛН
#51 This and Super Method in Java
12:11
Telusko
Рет қаралды 147 М.
Stream API in Java
26:04
Telusko
Рет қаралды 388 М.
#87 Thread Priority and Sleep in Java
7:29
Telusko
Рет қаралды 113 М.
#86 Multiple Threads in Java
11:30
Telusko
Рет қаралды 166 М.
#61 Abstract Keyword in Java
12:09
Telusko
Рет қаралды 199 М.
Generics In Java - Full Simple Tutorial
17:34
Coding with John
Рет қаралды 1,1 МЛН
#83 User Input using BufferedReader and Scanner in Java
11:48
34. Thread Pools in Java | ThreadPoolExecutor Framework | Multithreading Part6
1:16:55
Concept && Coding - by Shrayansh
Рет қаралды 54 М.