//********************************************************************* public class Main{
public static void main(String[] args) throws InterruptedException{ /* thread = A thread of execution in a program (kind of like a virtual CPU) The JVM allows an application to have multiple threads running concurrently Each thread can execute parts of you code in parallel with the main thread Each thread has a priority. Threads with higher priority are executed in preference compared to threads with a lower priority
The Java Virtual Machine continues to execute threads until either of the following occurs 1. The exit method of class Runtime has been called 2. All user threads have died
When a JVM starts up, there is a thread which calls the main method This thread is called “main”
Daemon thread is a low priority thread that runs in background to perform tasks such as garbage collection JVM terminates itself when all user threads (non-daemon threads) finish their execution */
//System.out.println(Thread.activeCount()); } } //********************************************************************* public class MyThread extends Thread{ @Override public void run() {
if(this.isDaemon()) { System.out.println("This is a daemon thread that is running"); } else { System.out.println("This is a user thread that is running"); } } } //*********************************************************************
Bro Code, please know you're out here saving lives, tears, grades, and reputations. I'm in an internship doing java programming and you're about 95% of the reason I've only cried once.
Hey man! Last summer I spent my vacation in Florida watching your channel; wrote a cool game. This summer I'm on assignment in Germany, and I'm back to watching your channel again. You have the perfect way of teaching Java! 10-15 minutes, examples, fun. Just great.
This was very useful after spending a couple days learning the basics of threads. If I began everything with watching this video, I wouldn't have known what was going on
I love your channel, thank you so much. You make things so concise. I come to your channel for every topic in my Java class and view the videos on it before going through my professors provided resources. It helps me tremendously. Bless you, my guy.
This video is extremely helpful and not hard to watch at all so far. I need this information but got tired of seeing 3 hour long videos drag on. So far so good. thank you for providing this content to me. I need it for a mod I'm trying to build.
I used thread in Client and server project I made. If many Client need to connect to one server, I created many thread to handle many different Client.
So I guess this is normal. But I still got confused. Because when thread2 starts you can no longer know for sure in which order things will happen. Sometimes thread 2 will finish before you have time to check if it is running or how many threads are running. And sometimes it will still be alive when you check it. Try adding a few more printline statements in both threads. Quite interesting to see how it will sometimes execute one line of code from one thread then jump to the other thread and so on. Also when making thread2 a daemon thread there is actually no guarantee that it will have time to execute all of its program before the Main thread finishes and thus shuts down the program!
I was wondering something about the run method. Why is it not always necessary to implement the Runnable interface? I often see threads getting created with the Runnable interface or the corresponding lambda expression. If the Thread class already has it's own "run" method, why do people implement the Runnable interface seperately?
Methods are actions that need to be taken place.. you can implement anything to do within the method. Let's say you need to execute that method 5 times in parallel. Then you can use threads.. threads will execute your method number of threads you have defined simultaneously.. if you did this using a loop it will execute one after another. Threads likes number of people doing the same function at the same time..instead of one handling the heavy load , multiple threads can handle the work load faster and efficiently with the cost of memory usage. And as bro taught you can use the thread execute the method or certain function with a certain time interval and so many other functions
//*********************************************************************
public class Main{
public static void main(String[] args) throws InterruptedException{
/*
thread = A thread of execution in a program (kind of like a virtual CPU)
The JVM allows an application to have multiple threads running concurrently
Each thread can execute parts of you code in parallel with the main thread
Each thread has a priority.
Threads with higher priority are executed in preference compared to threads with a lower priority
The Java Virtual Machine continues to execute threads until either of the following occurs
1. The exit method of class Runtime has been called
2. All user threads have died
When a JVM starts up, there is a thread which calls the main method
This thread is called “main”
Daemon thread is a low priority thread that runs in background to perform tasks such as garbage collection
JVM terminates itself when all user threads (non-daemon threads) finish their execution
*/
//System.out.println(Thread.activeCount());
//Thread.currentThread().setName("MAIN");
//System.out.println(Thread.currentThread().getName());
//Thread.currentThread().setPriority(10);
//System.out.println(Thread.currentThread().getPriority());
//System.out.println(Thread.currentThread().isAlive());
/*
for(int i =3;i>0;i--) {
System.out.println(i);
Thread.sleep(1000);
}
System.out.println("You are done!");
*/
//MyThread thread2 = new MyThread();
//thread2.setDaemon(true);
//System.out.println(thread2.isDaemon());
//thread2.start();
//System.out.println(thread2.isAlive());
//thread2.setName("2nd thread");
//System.out.println(thread2.getName());
//thread2.setPriority(1);
//System.out.println(thread2.getPriority());
//System.out.println(Thread.activeCount());
}
}
//*********************************************************************
public class MyThread extends Thread{
@Override
public void run() {
if(this.isDaemon()) {
System.out.println("This is a daemon thread that is running");
}
else {
System.out.println("This is a user thread that is running");
}
}
}
//*********************************************************************
You should create a website and post your code there, I think people would really appreciate it.
Ched
Bro Code, please know you're out here saving lives, tears, grades, and reputations. I'm in an internship doing java programming and you're about 95% of the reason I've only cried once.
Fantastic
Practicing...
public class Main
{
public static void main (String[]args) throws InterruptedException
{
//System.out.println(Thread.activeCount());
//Thread.currentThread().setName("Main");
//System.out.println(Thread.currentThread().getName());
//Thread.currentThread().setPriority(1);
//System.out.println(Thread.currentThread().getPriority());
//System.out.println(Thread.currentThread().isAlive());
/*for (int i = 8; i > 0; i--)
{
System.out.println (i);
Thread.sleep (2000);
}
System.out.println("Done!");
*/
MyThread thread2 = new MyThread();
thread2.setDaemon(true);
System.out.println(thread2.isDaemon());
thread2.start();
//System.out.println(thread2.isAlive());
//thread2.setName("Thread II");
//System.out.println(thread2.getName());
//thread2.setPriority(1);
//System.out.println(thread2.getPriority());
System.out.println(Thread.activeCount());
}
}
************
public class MyThread extends Thread{
@Override
public void run(){
if(this.isDaemon()){
System.out.println("Daemon thread is confirmed.");
}
else{
System.out.println("User thread is running.");
}
}
}
Hey man! Last summer I spent my vacation in Florida watching your channel; wrote a cool game. This summer I'm on assignment in Germany, and I'm back to watching your channel again. You have the perfect way of teaching Java! 10-15 minutes, examples, fun. Just great.
Hey! Hi matthew
which course did he teach about game bro
@@scotch5094 He only has beginner courses unfortunately but it would be great if he made advanced ones even if paid.
pure gold, these tutorials are genius. more tutorials please
by far the best java youtube channel
this teaching saved me one day of exercising on pdfs about threads
Absolute bro, this guy is singlehandedly building the future of the high-tech industry with his top quality videos ❣
Bro, you just saved my semester. You da best.
Us bro
Saved mine as well 💥💥
This was very useful after spending a couple days learning the basics of threads. If I began everything with watching this video, I wouldn't have known what was going on
thank you Bro :) I got my first job, I studied Java with your videos, super helpful.
I love your channel, thank you so much. You make things so concise. I come to your channel for every topic in my Java class and view the videos on it before going through my professors provided resources. It helps me tremendously. Bless you, my guy.
Thank you! I appreciate all the explanations and examples!
Virgin College: Takes hefty amout of fee but still doesn't know how to teach young minds
Chad Bro: *Simple explanation and Free of cost*
This video is extremely helpful and not hard to watch at all so far. I need this information but got tired of seeing 3 hour long videos drag on. So far so good. thank you for providing this content to me. I need it for a mod I'm trying to build.
simple yet effective. thanks for the lectures brah
I hope you keep doing these kind of videos
I used thread in Client and server project I made. If many Client need to connect to one server, I created many thread to handle many different Client.
your playlists are amazing . thank you !
Amazing Video
please Continue
Bro is genius in Java !
Very helpful content and thorough explanation. Thank you for sharing!👍
This guy is an absolute DAWGGGGGGGGGGGGG
Yooo
U have a great way for explaining!
Hit like to SUPPORT HIM, If you believe HE IS THE LIFE SAVER !!!!!!!
So I guess this is normal.
But I still got confused.
Because when thread2 starts you can no longer know for sure in which order things will happen.
Sometimes thread 2 will finish before you have time to check if it is running or how many threads are running.
And sometimes it will still be alive when you check it.
Try adding a few more printline statements in both threads.
Quite interesting to see how it will sometimes execute one line of code from one thread then jump to the other thread and so on.
Also when making thread2 a daemon thread there is actually no guarantee that it will have time to execute all of its program before the Main thread finishes and thus shuts down the program!
amazing video.u make my day
awesome tutorial!
I will forever keep coming back to your page for review and better understanding
you deserve a sub.
Thank you! Yup, helpful ;)
Awesome as usual!
Nice
Thanks bro
One of the best 👏🏼
Excellent video. Semester just started and this video really helped me out a lot man!.
Please be sure to continue making more videos. Thanks
Priceless. Thanks.
very useful video
Good sir 👍
best explanation!
This was well explained informative lesson. Thank you!
Thanks BRO !!
Thanks for the effort. Appreciate it.
Good job, Bro
helpful
Thank you friend!
Great video
It is good explenation i have ever listened
Simple, good explanation :)
Thank you for posting this -- the video is extremely useful.
Thanks for sharing!
OP stuff vrooo
I am AUSTINE BYRON from Kenya, please would you do a video on android connection to Neo4j database with java
Great video : )
extremely informative!
thanks for the class Bro
Bro you are great bro
Liked, by the way , there are so many people
thank you!
that was awesome thank you sensei
Excellent video. Thank you.
great video ! thank you very much !
useful 👍💙
awesome
Tnx bro you are the best
Two thumbs up! 👍👍
amazing
Thanks
good vid 👍
thx a lot bro
That's cooool!
well explained.. :)
Thank you
Hey thanks
But at the second println statement of thread.active count, I am getting 1 sometimes and 2 sometimes
Why?
I have started the thread 2 also
I was wondering something about the run method. Why is it not always necessary to implement the Runnable interface? I often see threads getting created with the Runnable interface or the corresponding lambda expression. If the Thread class already has it's own "run" method, why do people implement the Runnable interface seperately?
Thank you Bro :)
What is the purpose of threads?
Can you make a video about @Override?
ruclips.net/video/RpH50c2Z-Hc/видео.html
@@BroCodez thank you! Very good explanation!
Thanks bro
I'm dropping a comment down below for Bro.
BRO ♥
Thanks 😊
I don't understand what is a thread.. can someone explain?
A thread is a single sequential flow of control within a program
THX
What is the relationship/difference between threads and methods (and classes)?
Methods are actions that need to be taken place.. you can implement anything to do within the method. Let's say you need to execute that method 5 times in parallel. Then you can use threads.. threads will execute your method number of threads you have defined simultaneously.. if you did this using a loop it will execute one after another. Threads likes number of people doing the same function at the same time..instead of one handling the heavy load , multiple threads can handle the work load faster and efficiently with the cost of memory usage. And as bro taught you can use the thread execute the method or certain function with a certain time interval and so many other functions
Another banger
Thank You
nice!
How did you get your console text to be that colour?
Hey Bro
I want to be a fellow bro
NetBeans: sout👎
Eclipse: sysout👎
Sublime text: pl😎
Ly bro 12
My query is how to add JFrame in database please answer bro code
I haven't covered SQL yet, that may require a video to explain
ok
ok
14:08 when you are using the run method. How does the method know which thread it is checking?
couldn’t it be checking the main thread and not the one you just created?
just "bro"
hmmm so the when you call a thread it just runs all the functions? because you didnt call the run function
halo