//******************************************************* 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
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
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
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());
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?
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?
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!
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
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!
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.
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
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? 🙏🙏
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!
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.
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
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.
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.
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!
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.