Java multithreading 🧶

  Рет қаралды 123,220

Bro Code

Bro Code

Күн бұрын

Java multithreading tutorial
#java #multithreading #tutorial
//***************************************************************
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);
}
}
//***************************************************************

Пікірлер: 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."); } }
@wanke9837
@wanke9837 2 жыл бұрын
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
@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
@kareem1737
@kareem1737 2 жыл бұрын
Excellent explanation on both threading and multithreading. Thank you!
@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.
@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.
@stevancosovic4706
@stevancosovic4706 3 жыл бұрын
Thank you bro! Best programming teacher on youtube
@user-hr9dj3gm5u
@user-hr9dj3gm5u 2 жыл бұрын
This is the best video about multithreadind in java!
@Skillet367
@Skillet367 3 жыл бұрын
Good job man, thanks for the great work.
@hrishikeshmk6243
@hrishikeshmk6243 7 ай бұрын
Thanks for all the informative content you've got here bro
@darklegend7981
@darklegend7981 8 ай бұрын
An Hour worth spent learning !Thanks Bro
@TheElliotWeb
@TheElliotWeb 8 ай бұрын
Thank you! It's a great explanation of basic multithreading stuff what I found on KZbin.
@bosssmith9507
@bosssmith9507 2 жыл бұрын
Thanks, man this helped with my object-oriented programming class :]
@ricardorosa5315
@ricardorosa5315 3 жыл бұрын
Very very well explainned!!! Thank you very much!!!
@WickedChild95
@WickedChild95 2 жыл бұрын
Very good tutorial, thank you!
@comic-typ5919
@comic-typ5919 Жыл бұрын
You are really talented in teaching others, awesome.
@joelmarkjmj
@joelmarkjmj Жыл бұрын
You are my Favourite Java Professor forever
@niiazbekmamasaliev9828
@niiazbekmamasaliev9828 2 жыл бұрын
great channel, and great videos!!! good job man!
@levi25902
@levi25902 Жыл бұрын
Threads seemed very hard at campus, your video helped me understand it much better. Thank you for this amazing Video
@preraksemwal8768
@preraksemwal8768 2 жыл бұрын
Your channel is my favorite KZbin channel, due to many factors. PS:- your intro video rocks XD
@jasper5016
@jasper5016 3 жыл бұрын
Thanks for this awesome video. Subscribed.
@tirunagariuttam
@tirunagariuttam 2 жыл бұрын
Thanks for the nice explanation.
@user-yq5hj6lg3e
@user-yq5hj6lg3e 11 ай бұрын
Best of Best, sir. Thank you so mush.
@x3puair974
@x3puair974 3 жыл бұрын
Nice video. Very helpful. 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.
@PDSshinigami
@PDSshinigami 9 ай бұрын
i missed a class on this ,so i searched for it trying to find alex lee or someone relevant ,n im glad i found this channel ,now I got the syntax just have to do the logic until its second nature
@yuvalmuseri5464
@yuvalmuseri5464 Жыл бұрын
Sank you very much for da knowledge boi
@Cipher6i8
@Cipher6i8 2 жыл бұрын
Me: Starts learning about exception handling/threading in Java KZbin algorithm: Recommends the bro's videos on both custom exceptions and multi threading God bless you, bro.
@adrianlealcaldera2405
@adrianlealcaldera2405 3 жыл бұрын
very good, keep making videos man
@UninspiredFilm5
@UninspiredFilm5 14 күн бұрын
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!
@user-ru4xi2fo1t
@user-ru4xi2fo1t 2 ай бұрын
Buddy there is no one better than you , when it comes to helping in sem exams ❤
@korolek6442
@korolek6442 Жыл бұрын
Thank you for this video! it was really helpful :)
@felipeaugustoalves8388
@felipeaugustoalves8388 3 жыл бұрын
bem explicado, gostei
@nawfalnjm5699
@nawfalnjm5699 3 жыл бұрын
thank you, great video !
@oguzhantopaloglu9442
@oguzhantopaloglu9442 3 жыл бұрын
amazing!!!
@georgehusband3578
@georgehusband3578 3 жыл бұрын
Thanks Bro, great vids!!
@AzizbekFattoev
@AzizbekFattoev 7 ай бұрын
You are my real BRO who saved my entire semester
@MmdRsh
@MmdRsh Жыл бұрын
u are my hero man
@nitwaalebhaiya
@nitwaalebhaiya 2 жыл бұрын
Excellent Explanation.
@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 7 ай бұрын
Screw them
@semilife
@semilife 5 ай бұрын
Thanks Bro for your excellent resources.
@nizarouertani1315
@nizarouertani1315 3 жыл бұрын
i m ur new fan :D
@pranjalbajpai9956
@pranjalbajpai9956 2 жыл бұрын
You are awesome!
@calor6990
@calor6990 11 ай бұрын
Thank you!
@sean-qo4vc
@sean-qo4vc 2 жыл бұрын
Good job my guy Skuks(thanks)
@yousseffa5614
@yousseffa5614 2 жыл бұрын
you are awesome bro you made multithreading easy
@azamatdev
@azamatdev 3 ай бұрын
Yeah thank you so much i understand so much than i learnt payed one course. ❤
@emgm6207
@emgm6207 2 жыл бұрын
You are the best Bro!
@usmankabeer6776
@usmankabeer6776 3 жыл бұрын
Very Helpful VIdeo.
@alokendughosh7013
@alokendughosh7013 Жыл бұрын
Subscribed!! Keep going bro!!
@nicholasirvin5525
@nicholasirvin5525 Жыл бұрын
Great content man. I'm SMASHIN' that like button
@thugsmf
@thugsmf 2 жыл бұрын
Hey bro code, yeah I'm talking to you...lol.. love your teaching style! very practical examples! very easy to understand! the simplicity! never stop teaching! please. a fellow bro. thank you. ps. smashing that like button on every video
@dariocline
@dariocline 3 жыл бұрын
God bless you man
@AEINTech
@AEINTech 3 жыл бұрын
We want a video about Threads synchronization and scheduling please. By the way, the video is grate : )
@noahhuffman519
@noahhuffman519 Жыл бұрын
So Helpful
@Jan-fw7qz
@Jan-fw7qz 3 жыл бұрын
You're the best ❣️
@yashkapoor5894
@yashkapoor5894 Жыл бұрын
Good job my bro
@arcadia4087
@arcadia4087 3 жыл бұрын
Excellent explanation with practical code. Best than any other video on youtube. God bless you.
@kristjantoplana2993
@kristjantoplana2993 4 ай бұрын
Very good.
@ariefsaferman
@ariefsaferman 3 жыл бұрын
thanks help me a lot in thread
@-Corvo_Attano
@-Corvo_Attano Жыл бұрын
Bro Code is a gem :) Thank you *BRO*
@Monsta1291
@Monsta1291 2 жыл бұрын
amazing
@ecstatic6133
@ecstatic6133 2 жыл бұрын
Thanks ❣ Well explained
@daviddeguzman7218
@daviddeguzman7218 2 жыл бұрын
Keep up the good work Bro code!
@APDesignFXP
@APDesignFXP 2 жыл бұрын
just commenting to help u with the algorithm champ
@kemann3815
@kemann3815 2 жыл бұрын
Lovely
@mahmoudali1660
@mahmoudali1660 3 жыл бұрын
To the point👌
@user-fi2vc2wt5n
@user-fi2vc2wt5n Жыл бұрын
Cool!!
@leventeberry
@leventeberry 2 жыл бұрын
Great Video
@user-ci9om8vi9f
@user-ci9om8vi9f Жыл бұрын
Great thank you for such an enourmous work of creating 155 videos playlsit
@neil_armweak
@neil_armweak 3 жыл бұрын
Thanks Bro, very cool
@skyadav8842
@skyadav8842 2 жыл бұрын
awesome
@MrLoser-ks2xn
@MrLoser-ks2xn 2 жыл бұрын
Thanks
@matteocamarca
@matteocamarca 5 ай бұрын
THANKS.
@gilantonyborba3616
@gilantonyborba3616 10 ай бұрын
Thanksssssss againnnnn!!!💮🥀🌻🎴💮
@eugenezuev7349
@eugenezuev7349 28 күн бұрын
well done, Teacher
@orlinivanov7540
@orlinivanov7540 2 жыл бұрын
Ty Bro
@OneEgg42
@OneEgg42 3 жыл бұрын
Thank you Bro!
@pvc97
@pvc97 2 жыл бұрын
Tks you
@srinivasan4999
@srinivasan4999 Жыл бұрын
thanks man
@Momo-qr3rd
@Momo-qr3rd 3 жыл бұрын
thank you very much Bro
@gamingisnotacrime6711
@gamingisnotacrime6711 Жыл бұрын
Dope🔥
@mdalladin7227
@mdalladin7227 Жыл бұрын
love u bro
@moni7388
@moni7388 3 жыл бұрын
thank you
@sadeqsz6076
@sadeqsz6076 10 ай бұрын
thanks
@imnithyn4447
@imnithyn4447 2 жыл бұрын
Dude👌
@dmitriinekhristov8334
@dmitriinekhristov8334 2 жыл бұрын
Thx bro!
@rahultandon9749
@rahultandon9749 2 жыл бұрын
GREAT DISCOVERY ON A WEEKEND
@lamias7712
@lamias7712 2 жыл бұрын
Thanks Bro
@_sf_editz1870
@_sf_editz1870 2 жыл бұрын
Sensei 😁
@shibildas
@shibildas Жыл бұрын
wow. just... wow
@ysf9423
@ysf9423 Жыл бұрын
thanks bro
@jonathanli7081
@jonathanli7081 3 жыл бұрын
Very good vid
@forspt6334
@forspt6334 2 жыл бұрын
thanks bro !!!
@gogoi.
@gogoi. 3 жыл бұрын
Thank u
@percivalgebashe4376
@percivalgebashe4376 Жыл бұрын
Nice
@leoxu7826
@leoxu7826 2 жыл бұрын
nice
@poruboi5924
@poruboi5924 Жыл бұрын
This saved me
@callmesuraj4257
@callmesuraj4257 2 жыл бұрын
super bro
Java packages 📦
4:40
Bro Code
Рет қаралды 60 М.
Java threads 🧵
16:01
Bro Code
Рет қаралды 107 М.
Survival skills: A great idea with duct tape #survival #lifehacks #camping
00:27
Did you believe it was real? #tiktok
00:25
Анастасия Тарасова
Рет қаралды 55 МЛН
Вечный ДВИГАТЕЛЬ!⚙️ #shorts
00:27
Гараж 54
Рет қаралды 14 МЛН
Java lambda λ
18:00
Bro Code
Рет қаралды 90 М.
Java Socket Programming - Multiple Clients Chat
40:18
WittCode
Рет қаралды 176 М.
The Flaws of Inheritance
10:01
CodeAesthetic
Рет қаралды 910 М.
Multithreading in Java Explained in 10 Minutes
10:01
Coding with John
Рет қаралды 893 М.
Java serialization 🥣
21:13
Bro Code
Рет қаралды 72 М.
Lambda Expressions in Java - Full Simple Tutorial
13:05
Coding with John
Рет қаралды 716 М.
Generics In Java - Full Simple Tutorial
17:34
Coding with John
Рет қаралды 1 МЛН
Java HashMap 🗺️
13:05
Bro Code
Рет қаралды 77 М.
Exception Handling in Java Tutorial
13:20
Coding with John
Рет қаралды 369 М.
Хотела заскамить на Айфон!😱📱(@gertieinar)
0:21
Взрывная История
Рет қаралды 6 МЛН
Как распознать поддельный iPhone
0:44
PEREKUPILO
Рет қаралды 1,7 МЛН
تجربة أغرب توصيلة شحن ضد القطع تماما
0:56
صدام العزي
Рет қаралды 51 МЛН
ОБСЛУЖИЛИ САМЫЙ ГРЯЗНЫЙ ПК
1:00
VA-PC
Рет қаралды 1,9 МЛН
Samsung Galaxy Unpacked July 2024: Official Replay
1:8:53
Samsung
Рет қаралды 23 МЛН
WATERPROOF RATED IP-69🌧️#oppo #oppof27pro#oppoindia
0:10
Fivestar Mobile
Рет қаралды 19 МЛН