Тёмный

Java multithreading 🧶 

Bro Code
Подписаться 2,2 млн
Просмотров 129 тыс.
50% 1

Опубликовано:

 

29 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 156   
@BroCodez
@BroCodez 4 года назад
//******************************************************* public class Main{ public static void main(String[] args) throws InterruptedException{ // Create a subclass of Thread MyThread thread1 = new MyThread(); //or //implement Runnable interface and pass instance as an argument to Thread() MyRunnable runnable1 = new MyRunnable(); Thread thread2 = new Thread(runnable1); //thread1.setDaemon(true); //thread2.setDaemon(true); thread1.start(); //thread1.join(); //calling thread (ex.main) waits until the specified thread dies or for x milliseconds thread2.start(); //System.out.println(1/0); } } //******************************************************* public class MyThread extends Thread{ @Override public void run() { for(int i =10;i>0;i--) { System.out.println("Thread #1 : "+i); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("Thread #1 is finished :)"); } } //******************************************************* public class MyRunnable implements Runnable{ @Override public void run() { for(int i =0;i
@Sorjen108
@Sorjen108 2 года назад
Hello I have a question How would you create a multi-threaded program with 2 FOR loops, and change the priority of the loops to make the second loop terminate before the first loop? I have tried to use the set priority but to avail, the first FOR loop always gets executed first
@jojovstojo
@jojovstojo Год назад
public class Main{ public static void main(String[] args) throws InterruptedException{ //1st Way of creating a Thread :: Create a subclass of Thread class MyThread thread1 = new MyThread(); //or //2nd Way of creating a Thread :: Implement Runnable interface and pass instance as an argument to Thread() MyRunnable runnable1 = new MyRunnable(); Thread thread2 = new Thread(runnable1); thread1.setDaemon(true); thread2.setDaemon(true); //Normally, when thread1 & thread2 are not daemon threads -- in that case, even if an exception occurs in the 'main' thread, the other two threads will continue to run (& complete) without any interruption. But, if we make the threads - thread1 & thread2 - into daemon threads - then in that case there remains only one primary/user thread i.e 'main' thread. An then, if any exception occurs in our one and only user/primary thread i.e main thread, then the compiler does not care to complete the execution of daemon threads (threads1 & thread2) and the whole program immediately stops. thread1.start(); thread1.join(); //this line of code makes the 'main' thread wait/pause untill the thread1 completes its excecution. And once thread1 completes its execution, the main thread continues again -- i.e starts threads2. // thread1.join(3000); //this line of code makes the 'main' thread wait/pause for 3000 milliseconds (after starting of thread1) before continuing its execution -- i.e starting threads2. thread2.start(); //An important point to note here is that even if there occurs an exception during execution of one of the threads, the other thread/threads continue to run without any problem. System.out.println(1/0); //This will cause an exception in 'main' thread but the threads 'thread1' and 'thread2' will continue to run without any interruption. But, if threads - thread1 & thread2 - are made into daemon threads, then in that case there remains only one primary/user thread i.e 'main' thread. Then, as soon as the exception occurs in the main thread (1/0), the compiler does not care to complete the execution of daemon threads (threads1 & thread2) and the whole program immediately stops. } } ********************************************************************************************************************** public class MyThread extends Thread{ @Override public void run() { //When we start an instance of this thread, the code inside run() function executes. run() is a function of Thread class. for(int i =10;i>0;i--) { System.out.println("Thread #1 : "+i); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("Thread #1 is finished :)"); } } ********************************************************************************************************************* public class MyRunnable implements Runnable{ @Override public void run() { for(int i =0;i
@joyceasante8292
@joyceasante8292 Год назад
Practicing... 1st Method(Creating a subclass of the the tread class) public class Main { public static void main(String[] args) { MyThread thread1 = new MyThread(); } } *************************************** public class MyThread extends Thread{ @Override public void run(){ for(int i=10; i>0; i--){ System.out.println("First thread : "+i); try{ Thread.sleep(2000); } catch(InterruptedException e) { e.printStackTrace(); } } System.out.println("First thread is completed."); } } _____________________________________________ 2nd Method(Creating a class that implements the runnable interface) public class Main { public static void main(String[] args) { MyThread thread1 = new MyThread(); MyRunnable runnable1 = new MyRunnable(); Thread thread2 = new Thread(runnable1); } } **************** public class MyThread extends Thread{ @Override public void run(){ for(int i=10; i>0; i--){ System.out.println("First thread : "+i); try{ Thread.sleep(2000); } catch(InterruptedException e) { e.printStackTrace(); } } System.out.println("First thread is completed."); } } ********************* public class MyRunnable implements Runnable{ @Override public void run(){ } } ______________________ Successfully Multithreading public class Main { public static void main(String[] args) { MyThread thread1 = new MyThread(); MyRunnable runnable1 = new MyRunnable(); Thread thread2 = new Thread(runnable1); thread1.start(); thread2.start(); } } ******************* public class MyThread extends Thread{ @Override public void run(){ for(int i=10; i>0; i--){ System.out.println("First thread : "+i); try{ Thread.sleep(2000); } catch(InterruptedException e) { e.printStackTrace(); } } System.out.println("First thread is completed."); } } ************************** public class MyRunnable implements Runnable { @Override public void run () { for (int i = 0; i < 10; i++) { System.out.println ("Second thread : " + i); try { Thread.sleep (2000); } catch (InterruptedException e) { e.printStackTrace (); } } System.out.println("Second thread is completed."); } } ___________________________________ Final Code public class Main { public static void main(String[] args) throws InterruptedException { MyThread thread1 = new MyThread(); MyRunnable runnable1 = new MyRunnable(); Thread thread2 = new Thread(runnable1); //thread1.setDaemon(true); //thread2.setDaemon(true); thread1.start(); //thread1.join(5000); thread2.start(); System.out.println(2/0); } } ****************************** public class MyThread extends Thread{ @Override public void run(){ for(int i=10; i>0; i--){ System.out.println("First thread : "+i); try{ Thread.sleep(2000); } catch(InterruptedException e) { e.printStackTrace(); } } System.out.println("First thread is completed."); } } ********************************** public class MyRunnable implements Runnable { @Override public void run () { for (int i = 0; i < 10; i++) { System.out.println ("Second thread : " + i); try { Thread.sleep (2000); } catch (InterruptedException e) { e.printStackTrace (); } } System.out.println("Second thread is completed."); } }
@redpepper74
@redpepper74 8 месяцев назад
There's an easier way to make threads! Runnable is a @FunctionalInterface, which means that you can replace your custom Runnable with a function that takes no arguments and returns nothing. It's much nicer: Thread thread1 = new Thread(MyClass::myProcedure); Or you can write it as a lambda expression if you prefer this syntax: Thread thread2 = new Thread(() -> myProcedure());
@yashkapoor5894
@yashkapoor5894 Год назад
Good job my bro
@FaridHasanaliyev
@FaridHasanaliyev Год назад
Great thank you for such an enourmous work of creating 155 videos playlsit
@arcadia4087
@arcadia4087 3 года назад
Excellent explanation with practical code. Best than any other video on youtube. God bless you.
@TareqRahaman-u6x
@TareqRahaman-u6x Год назад
Best of Best, sir. Thank you so mush.
@gamingisnotacrime6711
@gamingisnotacrime6711 Год назад
Dope🔥
@MmdRsh
@MmdRsh Год назад
u are my hero man
@MrLoser-ks2xn
@MrLoser-ks2xn 2 года назад
Thanks
@rbdtrades9790
@rbdtrades9790 Год назад
Hows it going everybody, it's your bro here
@leventeberry
@leventeberry 3 года назад
Great Video
@superraegun2649
@superraegun2649 Год назад
wouldn't it be better to name your threads by passing an int into the class and then printing that int, that way you could have as many as you wanted and each would get a different name?
@matteocamarca
@matteocamarca 8 месяцев назад
THANKS.
@georgehusband3578
@georgehusband3578 3 года назад
Thanks Bro, great vids!!
@mdalladin7227
@mdalladin7227 Год назад
love u bro
@jonathanli7081
@jonathanli7081 3 года назад
Very good vid
@neil_armweak
@neil_armweak 3 года назад
Thanks Bro, very cool
@ysf9423
@ysf9423 Год назад
thanks bro
@mohamadhoseinbemani
@mohamadhoseinbemani 9 месяцев назад
my bro
@dariocline
@dariocline 3 года назад
God bless you man
@Jin._.20
@Jin._.20 Год назад
can you just do Thread t = new Thread(); and would that be the third way to create a thread? (not extending or implement anything) I've heard that if you craete a thread like this way, you need to use lambda expression to tell the program what that thread does instead of overriding run()... Am I correct?
@callmesuraj4257
@callmesuraj4257 3 года назад
super bro
@alanriyansa1425
@alanriyansa1425 3 года назад
ALLHAMDULILLAH
@sascories
@sascories 3 года назад
aowkaowkoakwoakwoakwoka
@muhammadyasiralfaruq7762
@muhammadyasiralfaruq7762 3 года назад
Yarhamukallah
@chillfill4866
@chillfill4866 2 года назад
How to debug with threads?
@augischadiegils.5109
@augischadiegils.5109 3 года назад
@strikerzzed0562
@strikerzzed0562 Год назад
bro code = my father
@geezcode
@geezcode 3 месяца назад
whay i donot understan my ticher but you!?
@abdullahalmahmud2146
@abdullahalmahmud2146 2 года назад
comment dropped
@natnael_esk
@natnael_esk 3 месяца назад
code family , code bro
@danielmilewski7659
@danielmilewski7659 2 года назад
comment for stats
@RohitKudiya1
@RohitKudiya1 11 месяцев назад
What is your name bro
@Freddie-uk9wk
@Freddie-uk9wk 3 месяца назад
a
@wanke9837
@wanke9837 3 года назад
You are absolutely the best teacher for me. I appreciate your teaching methodology and Thanks for everything you put into this course.
@hgatl
@hgatl 3 года назад
This is definitely the best description of many videos that i have watched, super
@adrian_vsk7203
@adrian_vsk7203 3 года назад
I tried to learn multithreading on a Udemy course and it seemed super complicated and difficult. This video made it so much simpler and provides really clear examples. Thanks so much Bro! Keep up the great work!
@ban_droid
@ban_droid 3 года назад
Sorry for your money that have been lost to pay that udemy course, lol 😂 i'm glad i'm checking every tutorial on youtube first if its exist for free 😂
@Lilyofc
@Lilyofc 9 месяцев назад
Screw them
@AEINTech
@AEINTech 3 года назад
We want a video about Threads synchronization and scheduling please. By the way, the video is grate : )
@Dramox.
@Dramox. 3 года назад
you're doing a better job explaining than my college professors... What's the point of college if I can just sit at home and watch endless youtube videos and learn more for less
@thedeveloper643
@thedeveloper643 3 года назад
you're helping me way more than this super thick book that i bought a couple days ago the book is probably good to someone else but it explains the same concepts in a complicated way with all the fancy words but you make it easier to understand showing simple examples it's ironic that it's easier for me to understand something in language that's not my mother tongue thank you for great videos! liked and subscribed love from south korea!
@adityarajsrivastava6580
@adityarajsrivastava6580 3 года назад
It is not strange for Hindi is my mother tongue but he gives various hilarious examples which make it easier to learn coding.
@kenkinyua7036
@kenkinyua7036 2 года назад
thanks so much ,,,is there sth i should add before setting a sub thread to daemon it's prompting an error kindly help?
@Cipher6i8
@Cipher6i8 2 года назад
Me: Starts learning about exception handling/threading in Java RU-vid algorithm: Recommends the bro's videos on both custom exceptions and multi threading God bless you, bro.
@Genesis-dj7kw
@Genesis-dj7kw 2 года назад
I must admit, I used to have no damn clue in class about java. Then I bought a Udemy course, which I found better to understand, yet still confusing at times. And now I stumbled upon your tutorials for Java; straight to the point, simple, yet applicable examples and all I need to pass my exams and actually enjoy coding. thank you so much :D
@shayo6251
@shayo6251 3 года назад
Hmm what if you wanted to create like 500+ threads?
@nancydai8778
@nancydai8778 Год назад
If I have a zip outputstream which is shared by one thread of reading content-copying to zip outputstream and the other thread of writing zip outputstream to response, how do I ensure they are not blocking each other? 🙏🙏
@stormpegasus7360
@stormpegasus7360 3 года назад
I guess I am not failing java
@imadeveoper
@imadeveoper 10 месяцев назад
thing that confuse me is that we are overriding a method called run. but we don't have that method in parent class so what type of override it is????
@fokas360
@fokas360 Год назад
// Your method of teaching is incredible💢[14.06.23]
@DUYNGUYEN-ui9uz
@DUYNGUYEN-ui9uz Год назад
Could you explain why we should not use inner class when creating thread? For example, I would write: Thread thread1 = new Thread(){ @Override public void run() { //do something } } Thank you!
@rionhoxha123gmail
@rionhoxha123gmail 8 месяцев назад
Using an anonymous inner class directly inside the Thread constructor may lead to less clean and less encapsulated code. It mixes the thread logic with the class definition. Separating the thread logic into a separate class or implementing Runnable allows for better encapsulation and cleaner code organization.
@sathwikcm5621
@sathwikcm5621 Год назад
can anyone explain why we have to put try and catch at 3:50 please , i'm getting confused
@joelmarkjmj
@joelmarkjmj Год назад
You are my Favourite Java Professor forever
@azamatdev
@azamatdev 5 месяцев назад
Yeah thank you so much i understand so much than i learnt payed one course. ❤
@treebit
@treebit 4 года назад
Hey, Bro, nice job! I watch ur tutorials not even being english speaker and learn a lot! Also do u plan to record JavaScript course? This would be awesome! PS sorry for cringy english
@BroCodez
@BroCodez 4 года назад
Your English is good. I understood. Yeah I'm hopefully going to add more Python and Javascript videos in the next few weeks. I would like to bring this Java playlist to 100 videos first. Shouldn't take too long.
@xvesderx6759
@xvesderx6759 День назад
thanks for your great tutorial
@Makariush
@Makariush Год назад
Take my energy Bro, beat the almighty RU-vid algorithm.
@semilife
@semilife 7 месяцев назад
Thanks Bro for your excellent resources.
@yuvalmuseri5464
@yuvalmuseri5464 Год назад
Sank you very much for da knowledge boi
@darklegend7981
@darklegend7981 11 месяцев назад
An Hour worth spent learning !Thanks Bro
@honoredegg
@honoredegg 2 года назад
87th. Thank you, ma Bro Sensei
@APDesignFXP
@APDesignFXP 2 года назад
just commenting to help u with the algorithm champ
@-Corvo_Attano
@-Corvo_Attano Год назад
Bro Code is a gem :) Thank you *BRO*
@stevancosovic4706
@stevancosovic4706 3 года назад
Thank you bro! Best programming teacher on youtube
@sean-qo4vc
@sean-qo4vc 2 года назад
Good job my guy Skuks(thanks)
@markus_code
@markus_code 2 года назад
Thank You for examples. I think now I can run my code without any sluggish methods. But it's not just that I think also I will change structure and logic of my program. D.amn thank you for opening my eyes.
@UninspiredFilm5
@UninspiredFilm5 3 месяца назад
Spent the whole day trying to make a program work with multiple threads in a method that made no sense, started fresh and this video made it work just right!
@gilantonyborba3616
@gilantonyborba3616 Год назад
Thanksssssss againnnnn!!!💮🥀🌻🎴💮
@deatharw1997
@deatharw1997 3 года назад
SUBSCRIBED TO BECOME FELLOW BRO xD
@TheElliotWeb
@TheElliotWeb 10 месяцев назад
Thank you! It's a great explanation of basic multithreading stuff what I found on RU-vid.
@erenyeager4452
@erenyeager4452 2 года назад
commenting for the algo.
@bosssmith9507
@bosssmith9507 2 года назад
Thanks, man this helped with my object-oriented programming class :]
@preraksemwal8768
@preraksemwal8768 2 года назад
Your channel is my favorite RU-vid channel, due to many factors. PS:- your intro video rocks XD
@eugenezuev7349
@eugenezuev7349 3 месяца назад
well done, Teacher
@nizarouertani1315
@nizarouertani1315 3 года назад
i m ur new fan :D
@levi25902
@levi25902 Год назад
Threads seemed very hard at campus, your video helped me understand it much better. Thank you for this amazing Video
@IIITL-CSAI-2023-PC-0
@IIITL-CSAI-2023-PC-0 4 месяца назад
Buddy there is no one better than you , when it comes to helping in sem exams ❤
@furkanveliisk4113
@furkanveliisk4113 2 года назад
brocode lets goooo
@korolek6442
@korolek6442 Год назад
Thank you for this video! it was really helpful :)
@Mr.Legend_9
@Mr.Legend_9 2 года назад
Thanks bro,Ive learned a lot from you...when i first see your videos i dont know anything regarding coding but now i have made my own apps.I came again because i forgot threading basics and my new app really needs it.Keep it up,I Wish you have a happy life.
@southasiandad8
@southasiandad8 Год назад
koment
@_sf_editz1870
@_sf_editz1870 2 года назад
Sensei 😁
@oguzhantopaloglu9442
@oguzhantopaloglu9442 3 года назад
amazing!!!
@jasper5016
@jasper5016 3 года назад
Thanks for this awesome video. Subscribed.
@AzizbekFattoev
@AzizbekFattoev 9 месяцев назад
You are my real BRO who saved my entire semester
@marwanhussam1299
@marwanhussam1299 Месяц назад
good video
@hrishikeshmk6243
@hrishikeshmk6243 9 месяцев назад
Thanks for all the informative content you've got here bro
@x3puair974
@x3puair974 3 года назад
Nice video. Very helpful. Thanks
@poruboi5924
@poruboi5924 Год назад
This saved me
@calor6990
@calor6990 Год назад
Thank you!
@kristjantoplana2993
@kristjantoplana2993 6 месяцев назад
Very good.
@forspt6334
@forspt6334 3 года назад
thanks bro !!!
@Samuel-kq4ip
@Samuel-kq4ip 2 года назад
Bro is coding
@yousseffa5614
@yousseffa5614 3 года назад
you are awesome bro you made multithreading easy
@ВолодимирПасічник-ъ8щ
This is the best video about multithreadind in java!
@shibildas
@shibildas Год назад
wow. just... wow
@niiazbekmamasaliev9828
@niiazbekmamasaliev9828 3 года назад
great channel, and great videos!!! good job man!
@screenname96
@screenname96 Год назад
🙏💙
@lamias7712
@lamias7712 2 года назад
Thanks Bro
@alokendughosh7013
@alokendughosh7013 Год назад
Subscribed!! Keep going bro!!
@VANTYCSolutions
@VANTYCSolutions 3 года назад
Live Saver!
@MrLoser-ks2xn
@MrLoser-ks2xn Год назад
@sharathkumar726
@sharathkumar726 Год назад
@srinivasan4999
@srinivasan4999 Год назад
thanks man
Далее
Java packages 📦
4:40
Просмотров 65 тыс.
Java threads 🧵
16:01
Просмотров 113 тыс.
Watermelon magic box! #shorts by Leisi Crazy
00:20
Просмотров 18 млн
Почему?
00:22
Просмотров 176 тыс.
Java generics ❓
22:04
Просмотров 109 тыс.
Lambda Expressions in Java - Full Simple Tutorial
13:05
Java encryption program 🔑
32:06
Просмотров 45 тыс.
Multithreading in Java Explained in 10 Minutes
10:01
Просмотров 927 тыс.
Java serialization 🥣
21:13
Просмотров 76 тыс.
Java enum 🪐
10:50
Просмотров 66 тыс.
Handle 1,000,000 Threads with Java and Spring Boot !!!
21:50
Exception Handling in Java Tutorial
13:20
Просмотров 390 тыс.