Java Threads Tutorial 2 - How to Create Threads in Java by Extending Thread Class

preview_player
Показать описание
In this Video I am going to give an How to Create Threads in Java by Extending Thread Class in Java.

Extending the Thread class includes following steps
- must implement the run() method
- thread ends when run() method finishes
- call .start() to get the thread ready to run

Threads is an Individual and separate unit of execution that is part of a process. multiple threads can work together to accomplish a common goal.Threads allow the program to run tasks in parallel. In many cases threads need to be synchronized,
that is, be kept not to handle the same data in memory concurrently. There are cases in which a thread needs to wait for another thread before proceeding

★★★Top Online Courses From ProgrammingKnowledge ★★★

★★★ Online Courses to learn ★★★

★★★ Follow ★★★

DISCLAIMER: This video and description contains affiliate links, which means that if you click on one of the product links, I’ll receive a small commission. This help support the channel and allows us to continue to make videos like this. Thank you for the support!
Рекомендации по теме
Комментарии
Автор

I think you can't say sleep() method is responsible for maintaining concurrency. With Sleep() you can delay thread. Here it's only a single println statement so you are able to show the concurrent result. Also if the processor is single-core then concurrency may work.
But suppose there are more statements in the run() method than only a println statement and execution time is greater than the sleep time. So suppose, thread1 finished execution and goes to sleep. thread2 starts running and as execution time is greater than sleep time, thread1 awakes starts printing its intermediate results (even thread2 is still running in another processor core). So here, thread1 is printing twice before thread2 even starts producing its result.

sunshine
Автор

Appreciate your simple explanation on calling start() and not run() in main class

alchavez
Автор

Thank you so much! I have been waiting a long time for you to touch upon the threading aspect of coding. Please continue the good work.

timdavids
Автор

If anyone got a problem with numbers are not printing parallel, you have to use sleep() method inside the loop. And there is a good explanation why you can't see concurrency without using sleep() method in @Sunshine comment. Hope this might be a help

sanjumaheeshan
Автор

Great example for beginners, thank you @ProgrammingKnowledge

lofts
Автор

I included the sleep method in the for loop and that solved the problem for me

ventjemazzel
Автор

@ time 7:25 minute in video, the output sequence is : -
0
0
1
1
2
2
3
3
4
4
5 <---
6 <---
5 <---
6 <---
7 <---
8 <---
9 <---
7 <---
8 <---
9 <---
// these are the outputs there is difference on later stage

AyushKumar-dkhm
Автор

thank u so much...u did an amazing job man!! thumbs up!!!

ashwinibaburamamurthy
Автор

it was an awesome tutorial, Thank You

farikaimutwira
Автор

when i right click and goto source and goto override.implement method then there is no option like run() ...pl zhelp me

ashwinkarki
Автор

You have not explained so many questions. At first why were the two threads printing parallel, then it prints concurrently. And when you added the sleep function why did it print parallel?

fernandoluis
Автор

Love your accent, great video, thanks :))

GChris
Автор

Why priority was given to second thread when you used currentThread().getId()

Technith
Автор

I want to write a program which make a new thread for accepting positive number from a main thread and then it displays all numbers from 0 to that number.. can u help me??

sundasqazi
Автор

when i define thread objects and run them in same order my output changes everytime i run the program?

yagzyalcntas
Автор

How can I change the text of an JTextField in another Thread?

belaaaaastendemitteee
Автор

I am unable to run this thread class in my project and this is not visible in run configuration.

vaneetkumar
Автор

Thanks,
Can it happen that each of these two threads run in parallel (not in concurrency) on two different cores of the system? How actually thread scheduling happens?

mazenezzeddine
Автор

What is thread.sleep actually doing, please explain, ,

Imtiaazs
Автор

Did you ever find the answer to "why did you lost concurrency at all when you first added currentThread().getID()? "

PapaBubl