Java multithreading 🧶

Ғылым және технология

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
    @BroCodez4 жыл бұрын

    //******************************************************* 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 { 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 { System.out.println ("Second thread : " + i); try { Thread.sleep (2000); } catch (InterruptedException e) { e.printStackTrace (); } } System.out.println("Second thread is completed."); } }

  • @wanke9837
    @wanke98372 жыл бұрын

    You are absolutely the best teacher for me. I appreciate your teaching methodology and Thanks for everything you put into this course.

  • @hgatl
    @hgatl3 жыл бұрын

    This is definitely the best description of many videos that i have watched, super

  • @Genesis-dj7kw
    @Genesis-dj7kw2 жыл бұрын

    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

  • @thedeveloper643
    @thedeveloper6433 жыл бұрын

    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.

  • @adrian_vsk7203
    @adrian_vsk72033 жыл бұрын

    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

  • @kareem1737
    @kareem17372 жыл бұрын

    Excellent explanation on both threading and multithreading. Thank you!

  • @Mr.Legend_9
    @Mr.Legend_92 жыл бұрын

    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
    @Cipher6i82 жыл бұрын

    Me: Starts learning about exception handling/threading in Java KZread algorithm: Recommends the bro's videos on both custom exceptions and multi threading God bless you, bro.

  • @stevancosovic4706
    @stevancosovic47063 жыл бұрын

    Thank you bro! Best programming teacher on youtube

  • @preraksemwal8768
    @preraksemwal87682 жыл бұрын

    Your channel is my favorite KZread channel, due to many factors. PS:- your intro video rocks XD

  • @darklegend7981
    @darklegend79818 ай бұрын

    An Hour worth spent learning !Thanks Bro

  • @hrishikeshmk6243
    @hrishikeshmk62436 ай бұрын

    Thanks for all the informative content you've got here bro

  • @TheElliotWeb
    @TheElliotWeb7 ай бұрын

    Thank you! It's a great explanation of basic multithreading stuff what I found on KZread.

  • @UninspiredFilm5
    @UninspiredFilm510 күн бұрын

    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!

  • @ricardorosa5315
    @ricardorosa53153 жыл бұрын

    Very very well explainned!!! Thank you very much!!!

  • @niiazbekmamasaliev9828
    @niiazbekmamasaliev98282 жыл бұрын

    great channel, and great videos!!! good job man!

  • @user-hr9dj3gm5u
    @user-hr9dj3gm5u2 жыл бұрын

    This is the best video about multithreadind in java!

  • @comic-typ5919
    @comic-typ5919 Жыл бұрын

    You are really talented in teaching others, awesome.

  • @joelmarkjmj
    @joelmarkjmj Жыл бұрын

    You are my Favourite Java Professor forever

  • @bosssmith9507
    @bosssmith95072 жыл бұрын

    Thanks, man this helped with my object-oriented programming class :]

  • @markus_code
    @markus_code2 жыл бұрын

    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.

  • @nicholasirvin5525
    @nicholasirvin5525 Жыл бұрын

    Great content man. I'm SMASHIN' that like button

  • @Skillet367
    @Skillet3673 жыл бұрын

    Good job man, thanks for the great work.

  • @levi25902
    @levi25902 Жыл бұрын

    Threads seemed very hard at campus, your video helped me understand it much better. Thank you for this amazing Video

  • @jasper5016
    @jasper50163 жыл бұрын

    Thanks for this awesome video. Subscribed.

  • @AEINTech
    @AEINTech3 жыл бұрын

    We want a video about Threads synchronization and scheduling please. By the way, the video is grate : )

  • @thugsmf
    @thugsmf2 жыл бұрын

    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

  • @WickedChild95
    @WickedChild952 жыл бұрын

    Very good tutorial, thank you!

  • @PDSshinigami
    @PDSshinigami8 ай бұрын

    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

  • @x3puair974
    @x3puair9743 жыл бұрын

    Nice video. Very helpful. Thanks

  • @korolek6442
    @korolek6442 Жыл бұрын

    Thank you for this video! it was really helpful :)

  • @user-yq5hj6lg3e
    @user-yq5hj6lg3e10 ай бұрын

    Best of Best, sir. Thank you so mush.

  • @yuvalmuseri5464
    @yuvalmuseri5464 Жыл бұрын

    Sank you very much for da knowledge boi

  • @nizarouertani1315
    @nizarouertani13153 жыл бұрын

    i m ur new fan :D

  • @tirunagariuttam
    @tirunagariuttam2 жыл бұрын

    Thanks for the nice explanation.

  • @alokendughosh7013
    @alokendughosh7013 Жыл бұрын

    Subscribed!! Keep going bro!!

  • @sean-qo4vc
    @sean-qo4vc2 жыл бұрын

    Good job my guy Skuks(thanks)

  • @user-ru4xi2fo1t
    @user-ru4xi2fo1t2 ай бұрын

    Buddy there is no one better than you , when it comes to helping in sem exams ❤

  • @yashkapoor5894
    @yashkapoor5894 Жыл бұрын

    Good job my bro

  • @felipeaugustoalves8388
    @felipeaugustoalves83883 жыл бұрын

    bem explicado, gostei

  • @semilife
    @semilife5 ай бұрын

    Thanks Bro for your excellent resources.

  • @azamatdev
    @azamatdev3 ай бұрын

    Yeah thank you so much i understand so much than i learnt payed one course. ❤

  • @oguzhantopaloglu9442
    @oguzhantopaloglu94423 жыл бұрын

    amazing!!!

  • @adrianlealcaldera2405
    @adrianlealcaldera24053 жыл бұрын

    very good, keep making videos man

  • @nitwaalebhaiya
    @nitwaalebhaiya2 жыл бұрын

    Excellent Explanation.

  • @emgm6207
    @emgm62072 жыл бұрын

    You are the best Bro!

  • @calor6990
    @calor699011 ай бұрын

    Thank you!

  • @georgehusband3578
    @georgehusband35783 жыл бұрын

    Thanks Bro, great vids!!

  • @nawfalnjm5699
    @nawfalnjm56993 жыл бұрын

    thank you, great video !

  • @AzizbekFattoev
    @AzizbekFattoev7 ай бұрын

    You are my real BRO who saved my entire semester

  • @usmankabeer6776
    @usmankabeer67763 жыл бұрын

    Very Helpful VIdeo.

  • @yousseffa5614
    @yousseffa56142 жыл бұрын

    you are awesome bro you made multithreading easy

  • @pranjalbajpai9956
    @pranjalbajpai99562 жыл бұрын

    You are awesome!

  • @APDesignFXP
    @APDesignFXP2 жыл бұрын

    just commenting to help u with the algorithm champ

  • @Jan-fw7qz
    @Jan-fw7qz3 жыл бұрын

    You're the best ❣️

  • @-Corvo_Attano
    @-Corvo_Attano Жыл бұрын

    Bro Code is a gem :) Thank you *BRO*

  • @ecstatic6133
    @ecstatic61332 жыл бұрын

    Thanks ❣ Well explained

  • @noahhuffman519
    @noahhuffman519 Жыл бұрын

    So Helpful

  • @MmdRsh
    @MmdRsh Жыл бұрын

    u are my hero man

  • @arcadia4087
    @arcadia40873 жыл бұрын

    Excellent explanation with practical code. Best than any other video on youtube. God bless you.

  • @leventeberry
    @leventeberry2 жыл бұрын

    Great Video

  • @skyadav8842
    @skyadav88422 жыл бұрын

    awesome

  • @daviddeguzman7218
    @daviddeguzman7218 Жыл бұрын

    Keep up the good work Bro code!

  • @kemann3815
    @kemann38152 жыл бұрын

    Lovely

  • @kristjantoplana2993
    @kristjantoplana29934 ай бұрын

    Very good.

  • @Monsta1291
    @Monsta12912 жыл бұрын

    amazing

  • @dariocline
    @dariocline3 жыл бұрын

    God bless you man

  • @user-ci9om8vi9f
    @user-ci9om8vi9f Жыл бұрын

    Great thank you for such an enourmous work of creating 155 videos playlsit

  • @eugenezuev7349
    @eugenezuev734925 күн бұрын

    well done, Teacher

  • @ariefsaferman
    @ariefsaferman3 жыл бұрын

    thanks help me a lot in thread

  • @user-fi2vc2wt5n
    @user-fi2vc2wt5n Жыл бұрын

    Cool!!

  • @neil_armweak
    @neil_armweak3 жыл бұрын

    Thanks Bro, very cool

  • @mahmoudali1660
    @mahmoudali16603 жыл бұрын

    To the point👌

  • @percivalgebashe4376
    @percivalgebashe4376 Жыл бұрын

    Nice

  • @gamingisnotacrime6711
    @gamingisnotacrime6711 Жыл бұрын

    Dope🔥

  • @orlinivanov7540
    @orlinivanov75402 жыл бұрын

    Ty Bro

  • @matteocamarca
    @matteocamarca5 ай бұрын

    THANKS.

  • @_sf_editz1870
    @_sf_editz18702 жыл бұрын

    Sensei 😁

  • @srinivasan4999
    @srinivasan4999 Жыл бұрын

    thanks man

  • @MrLoser-ks2xn
    @MrLoser-ks2xn2 жыл бұрын

    Thanks

  • @pvc97
    @pvc972 жыл бұрын

    Tks you

  • @imnithyn4447
    @imnithyn44472 жыл бұрын

    Dude👌

  • @gilantonyborba3616
    @gilantonyborba361610 ай бұрын

    Thanksssssss againnnnn!!!💮🥀🌻🎴💮

  • @mdalladin7227
    @mdalladin7227 Жыл бұрын

    love u bro

  • @jonathanli7081
    @jonathanli70813 жыл бұрын

    Very good vid

  • @OneEgg42
    @OneEgg423 жыл бұрын

    Thank you Bro!

  • @dmitriinekhristov8334
    @dmitriinekhristov83342 жыл бұрын

    Thx bro!

  • @lamias7712
    @lamias77122 жыл бұрын

    Thanks Bro

  • @ysf9423
    @ysf9423 Жыл бұрын

    thanks bro

  • @leoxu7826
    @leoxu78262 жыл бұрын

    nice

  • @callmesuraj4257
    @callmesuraj42572 жыл бұрын

    super bro

  • @Momo-qr3rd
    @Momo-qr3rd3 жыл бұрын

    thank you very much Bro

  • @forspt6334
    @forspt63342 жыл бұрын

    thanks bro !!!

  • @shibildas
    @shibildas Жыл бұрын

    wow. just... wow

  • @sadeqsz6076
    @sadeqsz607610 ай бұрын

    thanks

  • @moni7388
    @moni73883 жыл бұрын

    thank you

  • @redpepper74
    @redpepper745 ай бұрын

    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());

  • @rahultandon9749
    @rahultandon97492 жыл бұрын

    GREAT DISCOVERY ON A WEEKEND

  • @gogoi.
    @gogoi.3 жыл бұрын

    Thank u

Келесі