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
//******************************************************* 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
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
Жыл бұрын
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
Жыл бұрын
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."); } }
You are absolutely the best teacher for me. I appreciate your teaching methodology and Thanks for everything you put into this course.
This is definitely the best description of many videos that i have watched, super
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
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
3 жыл бұрын
It is not strange for Hindi is my mother tongue but he gives various hilarious examples which make it easier to learn coding.
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
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
7 ай бұрын
Screw them
Excellent explanation on both threading and multithreading. Thank you!
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.
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.
Thank you bro! Best programming teacher on youtube
Your channel is my favorite KZread channel, due to many factors. PS:- your intro video rocks XD
An Hour worth spent learning !Thanks Bro
Thanks for all the informative content you've got here bro
Thank you! It's a great explanation of basic multithreading stuff what I found on KZread.
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!
Very very well explainned!!! Thank you very much!!!
great channel, and great videos!!! good job man!
This is the best video about multithreadind in java!
You are really talented in teaching others, awesome.
You are my Favourite Java Professor forever
Thanks, man this helped with my object-oriented programming class :]
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.
Great content man. I'm SMASHIN' that like button
Good job man, thanks for the great work.
Threads seemed very hard at campus, your video helped me understand it much better. Thank you for this amazing Video
Thanks for this awesome video. Subscribed.
We want a video about Threads synchronization and scheduling please. By the way, the video is grate : )
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
Very good tutorial, thank you!
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
Nice video. Very helpful. Thanks
Thank you for this video! it was really helpful :)
Best of Best, sir. Thank you so mush.
Sank you very much for da knowledge boi
i m ur new fan :D
Thanks for the nice explanation.
Subscribed!! Keep going bro!!
Good job my guy Skuks(thanks)
Buddy there is no one better than you , when it comes to helping in sem exams ❤
Good job my bro
bem explicado, gostei
Thanks Bro for your excellent resources.
Yeah thank you so much i understand so much than i learnt payed one course. ❤
amazing!!!
very good, keep making videos man
Excellent Explanation.
You are the best Bro!
Thank you!
Thanks Bro, great vids!!
thank you, great video !
You are my real BRO who saved my entire semester
Very Helpful VIdeo.
you are awesome bro you made multithreading easy
You are awesome!
just commenting to help u with the algorithm champ
You're the best ❣️
Bro Code is a gem :) Thank you *BRO*
Thanks ❣ Well explained
So Helpful
u are my hero man
Excellent explanation with practical code. Best than any other video on youtube. God bless you.
Great Video
awesome
Keep up the good work Bro code!
Lovely
Very good.
amazing
God bless you man
Great thank you for such an enourmous work of creating 155 videos playlsit
well done, Teacher
thanks help me a lot in thread
Cool!!
Thanks Bro, very cool
To the point👌
Nice
Dope🔥
Ty Bro
THANKS.
Sensei 😁
thanks man
Thanks
Tks you
Dude👌
Thanksssssss againnnnn!!!💮🥀🌻🎴💮
love u bro
Very good vid
Thank you Bro!
Thx bro!
Thanks Bro
thanks bro
nice
super bro
thank you very much Bro
thanks bro !!!
wow. just... wow
thanks
thank you
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());
GREAT DISCOVERY ON A WEEKEND
Thank u