Java multithreading 🧶

  Рет қаралды 122,026

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);
}
}
//***************************************************************

Пікірлер: 155
@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!
@hrishikeshmk6243
@hrishikeshmk6243 6 ай бұрын
Thanks for all the informative content you've got here bro
@niiazbekmamasaliev9828
@niiazbekmamasaliev9828 2 жыл бұрын
great channel, and great videos!!! good job man!
@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.
@Cipher6i8
@Cipher6i8 2 жыл бұрын
Me: Starts learning about exception handling/threading in Java KZfaq algorithm: Recommends the bro's videos on both custom exceptions and multi threading God bless you, bro.
@stevancosovic4706
@stevancosovic4706 3 жыл бұрын
Thank you bro! Best programming teacher on youtube
@preraksemwal8768
@preraksemwal8768 2 жыл бұрын
Your channel is my favorite KZfaq channel, due to many factors. PS:- your intro video rocks XD
@Skillet367
@Skillet367 3 жыл бұрын
Good job man, thanks for the great work.
@darklegend7981
@darklegend7981 7 ай бұрын
An Hour worth spent learning !Thanks Bro
@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 6 ай бұрын
Screw them
@nicholasirvin5525
@nicholasirvin5525 Жыл бұрын
Great content man. I'm SMASHIN' that like button
@TheElliotWeb
@TheElliotWeb 7 ай бұрын
Thank you! It's a great explanation of basic multithreading stuff what I found on KZfaq.
@bosssmith9507
@bosssmith9507 2 жыл бұрын
Thanks, man this helped with my object-oriented programming class :]
@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.
@ricardorosa5315
@ricardorosa5315 3 жыл бұрын
Very very well explainned!!! Thank you very much!!!
@user-hr9dj3gm5u
@user-hr9dj3gm5u 2 жыл бұрын
This is the best video about multithreadind in java!
@comic-typ5919
@comic-typ5919 Жыл бұрын
You are really talented in teaching others, awesome.
@jasper5016
@jasper5016 3 жыл бұрын
Thanks for this awesome video. Subscribed.
@joelmarkjmj
@joelmarkjmj Жыл бұрын
You are my Favourite Java Professor forever
@WickedChild95
@WickedChild95 2 жыл бұрын
Very good tutorial, thank you!
@alokendughosh7013
@alokendughosh7013 Жыл бұрын
Subscribed!! Keep going bro!!
@x3puair974
@x3puair974 3 жыл бұрын
Nice video. Very helpful. Thanks
@tirunagariuttam
@tirunagariuttam 2 жыл бұрын
Thanks for the nice explanation.
@korolek6442
@korolek6442 Жыл бұрын
Thank you for this video! it was really helpful :)
@levi25902
@levi25902 Жыл бұрын
Threads seemed very hard at campus, your video helped me understand it much better. Thank you for this amazing Video
@AEINTech
@AEINTech 3 жыл бұрын
We want a video about Threads synchronization and scheduling please. By the way, the video is grate : )
@user-yq5hj6lg3e
@user-yq5hj6lg3e 10 ай бұрын
Best of Best, sir. Thank you so mush.
@yuvalmuseri5464
@yuvalmuseri5464 Жыл бұрын
Sank you very much for da knowledge boi
@user-ru4xi2fo1t
@user-ru4xi2fo1t Ай бұрын
Buddy there is no one better than you , when it comes to helping in sem exams ❤
@sean-qo4vc
@sean-qo4vc 2 жыл бұрын
Good job my guy Skuks(thanks)
@adrianlealcaldera2405
@adrianlealcaldera2405 3 жыл бұрын
very good, keep making videos man
@PDSshinigami
@PDSshinigami 8 ай бұрын
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
@georgehusband3578
@georgehusband3578 3 жыл бұрын
Thanks Bro, great vids!!
@emgm6207
@emgm6207 2 жыл бұрын
You are the best Bro!
@oguzhantopaloglu9442
@oguzhantopaloglu9442 3 жыл бұрын
amazing!!!
@Dramox.
@Dramox. 2 жыл бұрын
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
@nitwaalebhaiya
@nitwaalebhaiya 2 жыл бұрын
Excellent Explanation.
@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
@AzizbekFattoev
@AzizbekFattoev 6 ай бұрын
You are my real BRO who saved my entire semester
@nizarouertani1315
@nizarouertani1315 3 жыл бұрын
i m ur new fan :D
@nawfalnjm5699
@nawfalnjm5699 3 жыл бұрын
thank you, great video !
@pranjalbajpai9956
@pranjalbajpai9956 2 жыл бұрын
You are awesome!
@semilife
@semilife 4 ай бұрын
Thanks Bro for your excellent resources.
@felipeaugustoalves8388
@felipeaugustoalves8388 3 жыл бұрын
bem explicado, gostei
@yousseffa5614
@yousseffa5614 2 жыл бұрын
you are awesome bro you made multithreading easy
@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.
@azamatdev
@azamatdev 2 ай бұрын
Yeah thank you so much i understand so much than i learnt payed one course. ❤
@usmankabeer6776
@usmankabeer6776 3 жыл бұрын
Very Helpful VIdeo.
@redpepper74
@redpepper74 5 ай бұрын
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());
@MmdRsh
@MmdRsh Жыл бұрын
u are my hero man
@APDesignFXP
@APDesignFXP 2 жыл бұрын
just commenting to help u with the algorithm champ
@noahhuffman519
@noahhuffman519 Жыл бұрын
So Helpful
@Jan-fw7qz
@Jan-fw7qz 3 жыл бұрын
You're the best ❣️
@dariocline
@dariocline 3 жыл бұрын
God bless you man
@arcadia4087
@arcadia4087 3 жыл бұрын
Excellent explanation with practical code. Best than any other video on youtube. God bless you.
@-Corvo_Attano
@-Corvo_Attano Жыл бұрын
Bro Code is a gem :) Thank you *BRO*
@yashkapoor5894
@yashkapoor5894 Жыл бұрын
Good job my bro
@daviddeguzman7218
@daviddeguzman7218 Жыл бұрын
Keep up the good work Bro code!
@ecstatic6133
@ecstatic6133 2 жыл бұрын
Thanks ❣ Well explained
@kristjantoplana2993
@kristjantoplana2993 3 ай бұрын
Very good.
@leventeberry
@leventeberry 2 жыл бұрын
Great Video
@user-ci9om8vi9f
@user-ci9om8vi9f Жыл бұрын
Great thank you for such an enourmous work of creating 155 videos playlsit
@calor6990
@calor6990 10 ай бұрын
Thank you!
@Monsta1291
@Monsta1291 Жыл бұрын
amazing
@neil_armweak
@neil_armweak 3 жыл бұрын
Thanks Bro, very cool
@ariefsaferman
@ariefsaferman 3 жыл бұрын
thanks help me a lot in thread
@mahmoudali1660
@mahmoudali1660 3 жыл бұрын
To the point👌
@eugenezuev7349
@eugenezuev7349 12 күн бұрын
well done, Teacher
@kemann3815
@kemann3815 2 жыл бұрын
Lovely
@skyadav8842
@skyadav8842 2 жыл бұрын
awesome
@OneEgg42
@OneEgg42 3 жыл бұрын
Thank you Bro!
@mdalladin7227
@mdalladin7227 Жыл бұрын
love u bro
@user-fi2vc2wt5n
@user-fi2vc2wt5n Жыл бұрын
Cool!!
@Momo-qr3rd
@Momo-qr3rd 3 жыл бұрын
thank you very much Bro
@jonathanli7081
@jonathanli7081 3 жыл бұрын
Very good vid
@gamingisnotacrime6711
@gamingisnotacrime6711 Жыл бұрын
Dope🔥
@gilantonyborba3616
@gilantonyborba3616 10 ай бұрын
Thanksssssss againnnnn!!!💮🥀🌻🎴💮
@imnithyn4447
@imnithyn4447 2 жыл бұрын
Dude👌
@callmesuraj4257
@callmesuraj4257 2 жыл бұрын
super bro
@MrLoser-ks2xn
@MrLoser-ks2xn Жыл бұрын
Thanks
@srinivasan4999
@srinivasan4999 Жыл бұрын
thanks man
@lamias7712
@lamias7712 2 жыл бұрын
Thanks Bro
@percivalgebashe4376
@percivalgebashe4376 Жыл бұрын
Nice
@leoxu7826
@leoxu7826 2 жыл бұрын
nice
@rahultandon9749
@rahultandon9749 2 жыл бұрын
GREAT DISCOVERY ON A WEEKEND
@dmitriinekhristov8334
@dmitriinekhristov8334 2 жыл бұрын
Thx bro!
@ysf9423
@ysf9423 Жыл бұрын
thanks bro
@_sf_editz1870
@_sf_editz1870 Жыл бұрын
Sensei 😁
@moni7388
@moni7388 3 жыл бұрын
thank you
@forspt6334
@forspt6334 2 жыл бұрын
thanks bro !!!
@shibildas
@shibildas Жыл бұрын
wow. just... wow
@unknown_5505
@unknown_5505 Жыл бұрын
// Your method of teaching is incredible💢[14.06.23]
@matteocamarca
@matteocamarca 5 ай бұрын
THANKS.
@sadeqsz6076
@sadeqsz6076 9 ай бұрын
thanks
@pvc97
@pvc97 2 жыл бұрын
Tks you
@gogoi.
@gogoi. 3 жыл бұрын
Thank u
Java packages 📦
4:40
Bro Code
Рет қаралды 59 М.
Java threads 🧵
16:01
Bro Code
Рет қаралды 106 М.
Vivaan  Tanya once again pranked Papa 🤣😇🤣
00:10
seema lamba
Рет қаралды 24 МЛН
I CAN’T BELIEVE I LOST 😱
00:46
Topper Guild
Рет қаралды 61 МЛН
MEGA BOXES ARE BACK!!!
08:53
Brawl Stars
Рет қаралды 34 МЛН
Java lambda λ
18:00
Bro Code
Рет қаралды 89 М.
Java Socket Programming - Multiple Clients Chat
40:18
WittCode
Рет қаралды 175 М.
Multithreading in Java Explained in 10 Minutes
10:01
Coding with John
Рет қаралды 885 М.
Java Threads - Creating, starting and stopping threads in Java
17:14
Python Decorators in 15 Minutes
15:14
Kite
Рет қаралды 427 М.
Lambda Expressions in Java - Full Simple Tutorial
13:05
Coding with John
Рет қаралды 709 М.
Java generics ❓
22:04
Bro Code
Рет қаралды 101 М.
Object-Oriented Programming is Embarrassing: 4 Short Examples
28:03
Samsung S24 Ultra professional shooting kit #shorts
0:12
Photographer Army
Рет қаралды 34 МЛН
ИГРОВОВЫЙ НОУТ ASUS ЗА 57 тысяч
25:33
Ремонтяш
Рет қаралды 256 М.