Java multithreading 🧶

preview_player
Показать описание
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);




}
}
//***************************************************************
Рекомендации по теме
Комментарии
Автор


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<10;i++) {
System.out.println("Thread #2 : "+i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("Thread #2 is finished :)");
}
}

BroCodez
Автор

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

wanke
Автор

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

hgatl
Автор

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!

adrian_vsk
Автор

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!

thedeveloper
Автор

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

Genesis-djkw
Автор

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!

UninspiredFilm
Автор

Me: Starts learning about exception handling/threading in Java

YouTube algorithm: Recommends the bro's videos on both custom exceptions and multi threading

God bless you, bro.

Cipheri
Автор

You are my Favourite Java Professor forever

joelmarkjmj
Автор

An Hour worth spent learning !Thanks Bro

darklegend
Автор

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

AEINTech
Автор

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

ruxifot
Автор

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

Dramox.
Автор

Thanks Bro for your excellent resources.

semilife
Автор

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

TheElliotWeb
Автор

Thank you bro! Best programming teacher on youtube

stevancosovic
Автор

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.

Mr.Legend_
Автор

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

redpepper
Автор

You are really talented in teaching others, awesome.

comic-typ
Автор

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

levi