Java threads 🧵

แชร์
ฝัง

ความคิดเห็น • 134

  • @BroCodez
    @BroCodez  4 ปีที่แล้ว +65

    //*********************************************************************
    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");
    }
    }
    }
    //*********************************************************************

    • @aquate9637
      @aquate9637 3 ปีที่แล้ว +7

      You should create a website and post your code there, I think people would really appreciate it.

    • @nikolamarkovic9906
      @nikolamarkovic9906 2 ปีที่แล้ว

      Ched

    • @spicynoodles3317
      @spicynoodles3317 2 ปีที่แล้ว +1

      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.

    • @yusuphmwaigomole3104
      @yusuphmwaigomole3104 ปีที่แล้ว

      Fantastic

    • @joyceasante8292
      @joyceasante8292 ปีที่แล้ว +1

      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.");
      }
      }
      }

  • @matthewbrightman3398
    @matthewbrightman3398 2 ปีที่แล้ว +38

    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.

    • @chsktricks9397
      @chsktricks9397 ปีที่แล้ว

      Hey! Hi matthew

    • @scotch5094
      @scotch5094 2 หลายเดือนก่อน

      which course did he teach about game bro

    • @SpeaksYourWord
      @SpeaksYourWord หลายเดือนก่อน

      @@scotch5094 He only has beginner courses unfortunately but it would be great if he made advanced ones even if paid.

  • @IdeeFixeGamer
    @IdeeFixeGamer 3 ปีที่แล้ว +15

    pure gold, these tutorials are genius. more tutorials please

  • @joelcruz2873
    @joelcruz2873 ปีที่แล้ว +1

    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

  • @nizarouertani1315
    @nizarouertani1315 3 ปีที่แล้ว +11

    by far the best java youtube channel

  • @screenname96
    @screenname96 ปีที่แล้ว +2

    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.

  • @srujansanga2689
    @srujansanga2689 8 หลายเดือนก่อน +1

    this teaching saved me one day of exercising on pdfs about threads

  • @affable.pebble
    @affable.pebble 22 ชั่วโมงที่ผ่านมา

    Thank you! I appreciate all the explanations and examples!

  • @nawfalnjm5699
    @nawfalnjm5699 3 ปีที่แล้ว +1

    your playlists are amazing . thank you !

  • @LostKitty64x
    @LostKitty64x ปีที่แล้ว +1

    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.

  • @furkanveliisk4113
    @furkanveliisk4113 ปีที่แล้ว +2

    simple yet effective. thanks for the lectures brah

  • @ibrahimylmaz8378
    @ibrahimylmaz8378 ปีที่แล้ว

    thank you Bro :) I got my first job, I studied Java with your videos, super helpful.

  • @envektro2519
    @envektro2519 3 ปีที่แล้ว +3

    I hope you keep doing these kind of videos

  • @lilit3552
    @lilit3552 2 ปีที่แล้ว

    This was well explained informative lesson. Thank you!

  • @ahmadal-hafi580
    @ahmadal-hafi580 2 ปีที่แล้ว

    Yooo
    U have a great way for explaining!

  • @jusun2009
    @jusun2009 3 หลายเดือนก่อน

    Very helpful content and thorough explanation. Thank you for sharing!👍

  • @jenkar5716
    @jenkar5716 ปีที่แล้ว +7

    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*

  • @mahdib9361
    @mahdib9361 3 ปีที่แล้ว +1

    Amazing Video
    please Continue

  • @GraceHerbert-bk4it
    @GraceHerbert-bk4it ปีที่แล้ว

    Awesome as usual!

  • @jrd3uCe
    @jrd3uCe 9 หลายเดือนก่อน

    I will forever keep coming back to your page for review and better understanding

  • @darianchan4649
    @darianchan4649 3 ปีที่แล้ว +1

    awesome tutorial!

  • @blabberq
    @blabberq 2 ปีที่แล้ว

    Simple, good explanation :)

  • @user-jp3le2lz6d
    @user-jp3le2lz6d 11 หลายเดือนก่อน

    Bro is genius in Java !

  • @ajaykotian5
    @ajaykotian5 2 ปีที่แล้ว

    Thank you for posting this -- the video is extremely useful.

  • @argyriskappa2789
    @argyriskappa2789 3 ปีที่แล้ว +26

    Bro, you just saved my semester. You da best.

  • @oguzhantopaloglu9442
    @oguzhantopaloglu9442 3 ปีที่แล้ว +1

    extremely informative!

  • @waedradi8552
    @waedradi8552 7 หลายเดือนก่อน

    Thanks bro
    One of the best 👏🏼

  • @faizalimuhammadzoda4731
    @faizalimuhammadzoda4731 2 ปีที่แล้ว

    Thanks for the effort. Appreciate it.

  • @ruantristancarlinsky3851
    @ruantristancarlinsky3851 ปีที่แล้ว

    Excellent video. Semester just started and this video really helped me out a lot man!.
    Please be sure to continue making more videos. Thanks

  • @esthermdzitiro31
    @esthermdzitiro31 3 ปีที่แล้ว

    Great video

  • @faizalimuhammadzoda4731
    @faizalimuhammadzoda4731 2 ปีที่แล้ว

    Thanks for sharing!

  • @sanayasamadhi70
    @sanayasamadhi70 ปีที่แล้ว

    amazing video.u make my day

  • @110dhruvpatel5
    @110dhruvpatel5 7 หลายเดือนก่อน

    you deserve a sub.

  • @AEINTech
    @AEINTech 3 ปีที่แล้ว +1

    Great video : )

  • @SpidahRidah
    @SpidahRidah 3 ปีที่แล้ว

    Excellent video. Thank you.

  • @essaalu7047
    @essaalu7047 3 ปีที่แล้ว

    useful 👍💙

  • @oscarjosefsson9300
    @oscarjosefsson9300 ปีที่แล้ว +1

    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!

  • @mateskyjanica7696
    @mateskyjanica7696 ปีที่แล้ว

    Good job, Bro

  • @girl6994
    @girl6994 4 ปีที่แล้ว +6

    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.

  • @JohnWickXD
    @JohnWickXD 2 ปีที่แล้ว

    OP stuff vrooo

  • @user-ke9vd6mx1n
    @user-ke9vd6mx1n 7 หลายเดือนก่อน

    best explanation!

  • @xpolentaedgex
    @xpolentaedgex ปีที่แล้ว

    thanks for the class Bro

  • @ofiregev
    @ofiregev 2 ปีที่แล้ว

    great video ! thank you very much !

  • @pitched7401
    @pitched7401 ปีที่แล้ว

    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?

  • @AbhijeetKumar-cm3jh
    @AbhijeetKumar-cm3jh 3 ปีที่แล้ว +2

    Thanks BRO !!

  • @adityavikramsingh802
    @adityavikramsingh802 3 หลายเดือนก่อน

    This guy is an absolute DAWGGGGGGGGGGGGG

  • @-zokzok-9455
    @-zokzok-9455 2 ปีที่แล้ว

    that was awesome thank you sensei

  • @NHCS_AMOGH_PATIL
    @NHCS_AMOGH_PATIL 2 ปีที่แล้ว

    Bro you are great bro

  • @calor6990
    @calor6990 10 หลายเดือนก่อน

    Thank you friend!

  • @user-hr9dj3gm5u
    @user-hr9dj3gm5u 2 ปีที่แล้ว

    It is good explenation i have ever listened

  • @arijitdas3958
    @arijitdas3958 2 ปีที่แล้ว +1

    Hit like to SUPPORT HIM, If you believe HE IS THE LIFE SAVER !!!!!!!

  • @matteocamarca
    @matteocamarca 5 หลายเดือนก่อน

    Priceless. Thanks.

  • @user-ke6vc7pk6x
    @user-ke6vc7pk6x 11 หลายเดือนก่อน

    very useful video

  • @AVGilboa
    @AVGilboa 2 ปีที่แล้ว

    thank you!

  • @thangavelpetchiappan4689
    @thangavelpetchiappan4689 2 ปีที่แล้ว

    well explained.. :)

  • @magxbeats
    @magxbeats ปีที่แล้ว

    Good sir 👍

  • @CJDaBomb69
    @CJDaBomb69 ปีที่แล้ว

    How did you get your console text to be that colour?

  • @Monsta1291
    @Monsta1291 2 ปีที่แล้ว

    amazing

  • @chocodonuts3644
    @chocodonuts3644 8 หลายเดือนก่อน

    helpful

  • @abdullahalmahmud2146
    @abdullahalmahmud2146 2 ปีที่แล้ว

    awesome

  • @behrangfattahi9328
    @behrangfattahi9328 2 ปีที่แล้ว

    Tnx bro you are the best

  • @shaynazoedeguzman7418
    @shaynazoedeguzman7418 ปีที่แล้ว

    Two thumbs up! 👍👍

  • @nehalayaaz9406
    @nehalayaaz9406 4 ปีที่แล้ว

    Thanks 😊

  • @percivalgebashe4376
    @percivalgebashe4376 ปีที่แล้ว

    Nice

  • @tuanhungpham449
    @tuanhungpham449 3 ปีที่แล้ว

    Thanks bro

  • @tanushthiru3389
    @tanushthiru3389 5 หลายเดือนก่อน

    good vid 👍

  • @ivy267
    @ivy267 ปีที่แล้ว

    thx a lot bro

  • @Momo-qr3rd
    @Momo-qr3rd 3 ปีที่แล้ว

    Thank you Bro :)

  • @user-oe8es7hu5h
    @user-oe8es7hu5h 6 หลายเดือนก่อน

    That's cooool!

  • @harshith_takkala
    @harshith_takkala 3 ปีที่แล้ว +5

    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

  • @osazeimohe7745
    @osazeimohe7745 2 ปีที่แล้ว

    Thank you

  • @kippenvogelchen9717
    @kippenvogelchen9717 3 ปีที่แล้ว

    nice!

  • @gogoi.
    @gogoi. 3 ปีที่แล้ว

    Thank You

  • @girl6994
    @girl6994 4 ปีที่แล้ว +2

    Liked, by the way , there are so many people

  • @gustavokenzomiyamoto2434
    @gustavokenzomiyamoto2434 ปีที่แล้ว

    What is the relationship/difference between threads and methods (and classes)?

    • @tharinduperera8758
      @tharinduperera8758 ปีที่แล้ว

      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

  • @sofianealloui
    @sofianealloui ปีที่แล้ว

    BRO ♥

  • @MrLoser-ks2xn
    @MrLoser-ks2xn ปีที่แล้ว

    Thanks

  • @kemann3815
    @kemann3815 2 ปีที่แล้ว

    Another banger

  • @yahelbraun5471
    @yahelbraun5471 4 ปีที่แล้ว +2

    Can you make a video about @Override?

    • @BroCodez
      @BroCodez  4 ปีที่แล้ว +2

      th-cam.com/video/RpH50c2Z-Hc/w-d-xo.html

    • @yahelbraun5471
      @yahelbraun5471 4 ปีที่แล้ว +2

      @@BroCodez thank you! Very good explanation!

  • @Bladermishal10
    @Bladermishal10 3 ปีที่แล้ว

    hmmm so the when you call a thread it just runs all the functions? because you didnt call the run function

  • @Makariush
    @Makariush ปีที่แล้ว

    THX

  • @tobrojekt27
    @tobrojekt27 2 ปีที่แล้ว

    if i set thread2 to daemon, it did not show up the message, that it is a daemon thread that is running. I needed to add thread2.join; after the thread2.start; statement, in order to execute the run() function of thread2 somehow

    • @tobrojekt27
      @tobrojekt27 2 ปีที่แล้ว

      and i dont know why it workd for u but not for me without the thread2.join

  • @_sf_editz1870
    @_sf_editz1870 2 ปีที่แล้ว

    I want to be a fellow bro

  • @Origon09
    @Origon09 2 ปีที่แล้ว

    14:08 when you are using the run method. How does the method know which thread it is checking?

    • @Origon09
      @Origon09 2 ปีที่แล้ว

      couldn’t it be checking the main thread and not the one you just created?

  • @carlosbernardo6531
    @carlosbernardo6531 ปีที่แล้ว

  • @MrLoser-ks2xn
    @MrLoser-ks2xn ปีที่แล้ว

  • @vitor1678
    @vitor1678 2 ปีที่แล้ว

    I'm dropping a comment down below for Bro.

  • @girl6994
    @girl6994 4 ปีที่แล้ว +1

    My teacher didn’t taught me anything about daemon thread. Now I know something such as GC ‘s thread is a daemon thread. But there are still more daemon thread right? Could you list them?

    • @BroCodez
      @BroCodez  4 ปีที่แล้ว

      That one was the only example that I could find that I know of. You can make a user thread a daemon thread and the benefit is that the JVM does not wait for Daemon threads before exiting, while it waits for user threads, it does not exit unless all the user threads finish their execution

    • @girl6994
      @girl6994 4 ปีที่แล้ว +1

      Bro Code but if JVM does not wait for daemon thread before exit , that sounds a little strange. But wait, I asked my teacher just now, he said in Chinese it calls 守护线程, which means thread protector thread, which means he will die if he’s lover dies.

    • @BroCodez
      @BroCodez  4 ปีที่แล้ว

      @@girl6994 Daemon threads do background tasks usually. We typically don't want the JVM to wait to exit for daemon threads. The JVM will wait for user threads however

    • @girl6994
      @girl6994 4 ปีที่แล้ว

      Bro Code He didn’t mention that daemon thread always run background . I heard it for the first time.

    • @girl6994
      @girl6994 4 ปีที่แล้ว +1

      Bro Code I understood. After your explanation. I didn’t know daemon thread well after that class. At that time I was very confused about why there is something calls daemon thread , but now I understood. Because Daemon threads do background task, and we don’t want JVM to wait to exit for daemon threads. No wonder it calls 守护线程in Chinese. It is a interesting translation though.

  • @yahelbraun5471
    @yahelbraun5471 4 ปีที่แล้ว +2

    I don't understand what is a thread.. can someone explain?

    • @BroCodez
      @BroCodez  4 ปีที่แล้ว +1

      A thread is a single sequential flow of control within a program

  • @ponanbalagan2988
    @ponanbalagan2988 3 ปีที่แล้ว +1

    My query is how to add JFrame in database please answer bro code

    • @BroCodez
      @BroCodez  3 ปีที่แล้ว

      I haven't covered SQL yet, that may require a video to explain

  • @davidgonzalez-ge7ol
    @davidgonzalez-ge7ol 2 ปีที่แล้ว

    GOLD

  • @manuelgonzalezpalafox2627
    @manuelgonzalezpalafox2627 ปีที่แล้ว

    Ly bro 12

  • @danielmilewski7659
    @danielmilewski7659 ปีที่แล้ว +1

    comment for stats

  • @ricchiuto27
    @ricchiuto27 2 ปีที่แล้ว

    ok

  • @user-sb9ld7ul5b
    @user-sb9ld7ul5b 6 หลายเดือนก่อน

    just "bro"

  • @redazaidane6780
    @redazaidane6780 2 ปีที่แล้ว

    halo

  • @YassineTamam
    @YassineTamam ปีที่แล้ว

    slay

  • @zari_723
    @zari_723 ปีที่แล้ว

    hi

  • @kristjantoplana2993
    @kristjantoplana2993 3 หลายเดือนก่อน

    HELLOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO

  • @kristjantoplana2993
    @kristjantoplana2993 3 หลายเดือนก่อน

    HELLOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO