13.7 Multithreading Synchronized Keyword

แชร์
ฝัง
  • เผยแพร่เมื่อ 25 ม.ค. 2025

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

  • @kartzoftvasu3162
    @kartzoftvasu3162 4 ปีที่แล้ว +98

    those trying to test this programs on your high end laptops, might see no matter u use a counter of 1000 or 5000 or even 10,000 to increment, you might still get the correct total of incremented value without even using SYNCHRONIZE keyword to sync the increment process between 2 threads. It just means your thread scheduler has a lot of resources available to be very efficient. In such cases, just increase the counter loop by a million (1,00,000) and then you will see the need of using the SYNCHRONIZE keyword as you will see an incorrect total. Hope it helps.

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

      thanks mate

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

      Thanks :)

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

      Another hint here: remove any "per iteration" console log. While in loop, let only the counting, otherwise writing to console will give enough time to threads so they behave almost "synchronized"

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

      Try writing it as this
      Thread1.start();
      Thread1.join();
      Thread2.start();
      Thread2.join();
      Result: if you took for loop for 1000 or 1000000 each
      = 2000 or 2000000 respectively
      Without synchronised key word

    • @FL-xc1wk
      @FL-xc1wk 7 หลายเดือนก่อน

      ​@@sidharthbisoi1492simply adding synchronized diminishes the need for thread.join. however both things do the same

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

    Notes: not synchronized method is not thread safe, which means multiple thread can access the same method at the same time. Making the thread synchronized means only one thread can use the method at a time.

    • @andrejtrozic509
      @andrejtrozic509 10 หลายเดือนก่อน +3

      So if you want a method that can be used by one thread at a time
      -then make that method synchronized()

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

      @@andrejtrozic509 i was asked this question on an interview..its very imp

  • @FedoraTime
    @FedoraTime 5 ปีที่แล้ว +67

    Very clear and simple example, thank you!

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

    champav basu... intha simple ga telchesav, thread safe synchronization yappati nundo doubt. simply superb. hyd lo unte kaludam we can have good biryani..

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

    Best Java Teacher in TH-cam 💯

  • @mohammedhammad2113
    @mohammedhammad2113 4 ปีที่แล้ว +10

    That is a gem of an explanation, thank you Sir!

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

    Bucky Roberts from India .
    Amazing tutorials sir!

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

    your videos are very good. perfect point-to-point explanation such as mechanical students are also getting interested.

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

    Great video, thank you! One of the best Java instructors

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

    t1.start();
    t1.join();
    t2.start();
    t2.join();
    will fetch the same result without using the synchronized key word.
    BUT...WHAT YOU HAVE DONE FOR THE PURPOSE OF EXPLAINING ITS A PERFECT EXAMPLE.

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

    Crisp and clear explanation!!

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

    great sir very clear explanation
    one of the best multithreading explanation on TH-cam

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

    YOU are most talented navin.. best and simple way to understand that how u elaborate the concepts..

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

    Awesome and very clear explanation and easy to understand

  • @420thlegioner8
    @420thlegioner8 5 ปีที่แล้ว +1

    Godlike explanation, CLEAR AND SIMPLE !

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

    Synchronized block is used to lock an object for any shared resource.
    Scope of synchronized block is smaller than the method.
    A Java synchronized block doesn't allow more than one JVM, to provide access control to a shared resource.

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

    What an explaination sir .Just Wow.

  • @darshankhatri7375
    @darshankhatri7375 5 ปีที่แล้ว

    Sir you are Legend for CS Students.

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

    Thank you so much for creating such a useful video series ....it's been so useful for me...

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

    thank u so much sir . such a beautiful explanation

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

    This *synchronized* is a very big performance hit":
    *** mainThreadCall...
    Count: 2000000000 Done in: 125 milliseconds
    *** callingWithOneSeparateThread...
    Count: 2000000000 Done in: 129 milliseconds
    *** callingWithTwoThreads... (not synchronized method)
    Count: 1013362697 Done in: 67 milliseconds
    *** callingWithTwoThreadsAndSynchronizedMethod...
    Count: 2000000000 Done in: 67621 milliseconds
    Also on some conference I've seen an example where the "synchronized" did not help. I don't remember what was the case, but they had to do smething else. I think it was some I/O issue or with databases. Just a guess.
    Two synchronized threads are 540 times slower than one thread in my example (2,000,000,000 calls). But when there's less calls, it works fine (but still slower than in one thread).

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

    Naveen,
    You are an awesome teacher!!

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

    Excellent video, thanks so much, you made it easy and simple to understand :)

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

    best channel ever

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

    awesome explanation with example

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

    this saved so much of my time! thank you so much!

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

    explanation was crystal clear. Thanks a ton!

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

    You saved me so much time...

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

    absolute best !!!!!telusko🔥🔥🔥🔥

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

    Excellent video, makes understanding very simple and easy!!

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

    Awesomeee.... so concise and perfect explanation

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

    Thank you sir really it was helped me to understand this concept clearly

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

    Great example brother

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

    @Telusko
    We need teachers like you in college : )

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

    Ahhhh Bossss! What an explanation!

  • @krazybanda
    @krazybanda 6 ปีที่แล้ว +5

    if u use
    t1.start ();
    t1.join ();
    t2.start ();
    t2.join ();
    then there is no need of synchronised method
    btw u r a good teacher

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

      In that case, its not multithreading because until t1 finish main thread can't create t2 thread so it will become sequential execution instead of multithreading .

    • @kidslearningscience
      @kidslearningscience 5 ปีที่แล้ว

      In that case its not parallel processing .

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

      all of you are correct. because in a multithreading application, there comes a time where another thread requires an object or value from another thread for processing. in that case, this comes in handy.

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

    Well explained and to the point thank you so much for the amazing video!

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

    Thank you for explaining it comprehensively.

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

    Sir, I have a doubt
    when use threads like this
    t1.start();
    t1.join();
    t2.start();
    t2.join();
    we get 2000 without "synchronized " keyword

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

      When u call join then it simply means main thread goes to sleep state and t1.join is running unless t1 not completely execute (dead) .
      When first thread is done his work then join wakeup the main thread and call next thread so it means our program has only one thread when we use join method . But we want running both thread parallel.
      Basically due to join delay our program so we don't use it in program generally....

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

      Its like normal function called 2 times .
      Imagine that is a
      class vinayak{
      private int counter=0;
      void increment(){
      loop 1000 times {counter++;}
      }
      main class(){
      vinayak.increment();
      vinayak.increment();
      }
      then the counter will stil be 2000. To avoid this we use threads for parallel process.
      many threads should not use the increment function at once. so to provide accessibility to one thread at a time.

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

    wow! crystal clear explanation.THANK YOU!

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

    An amateur is asking this :
    What's the Point of thread concept then???

    • @nikilmanu334
      @nikilmanu334 20 วันที่ผ่านมา +1

      probably late but rest of the code in threads (before and after the method call) run simultaneously.

  • @chandu.l
    @chandu.l 8 หลายเดือนก่อน

    @5:24 join() still presented for both right then why it's not giving 2000

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

    Brilliant explanation, thanks for sharing!

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

    Good Explanation.. Thanks

  • @sairajdas6692
    @sairajdas6692 6 ปีที่แล้ว

    Excellent explanation sir

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

    Crystal clear!!!much appreciated!

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

    Our aim for using multithreading was parallel computing, but here we are using a single thread at a time. So how are we achieving that?

  • @satishkrajendran8703
    @satishkrajendran8703 7 ปีที่แล้ว

    Its really great. kudos to navin reddy

  • @baby0nboard-369
    @baby0nboard-369 3 ปีที่แล้ว

    Can you make a video about hibernate? and lambda? thank youuuu

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

    Great explanation....!!! Thank you.

  • @karthick2237
    @karthick2237 5 ปีที่แล้ว

    Top class teaching ..👏👏👍

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

    What happens when you call an unsynchronized method inside a synchronized method? Will the output be consistent?

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

      Thanks for this question.
      Because of this question, i tried calling unsynchronized method inside a synchronized method.
      The output is consistent.

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

    thankyou so much for great explaination

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

    i learnt three things
    1. Synchronized keyword
    2. join method
    3. Thread safe class

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

    Hi navin, I have one question in this concept of threading. My question is like let us have three threads t1, t2,t3. t1 shud print 1, t2 shud print 2, t3 shud print 3 , again t1 shud print 4, t2 shud 5 , t3 shud print 6 and so on like this .
    class Hi extends Thread
    {
    public synchronized void run(){
    for(int i=1;i

  • @sedittak
    @sedittak 5 ปีที่แล้ว

    Great video, nice explanation

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

    Thanks! great explanation on this topic!

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

    Thank you very much for this very informative and clear video. It helped me a lot :D

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

    A question, why you didn’t use implements or extends ?
    Thank you for the amazing video

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

      Because he didn't make new classes for custom threads, he used the classes Thread and Runnable themselves, a month late but well...

  • @axdayrboom3284
    @axdayrboom3284 6 ปีที่แล้ว

    thaaaaaaanks very much & I hope you'll explain the Thread pool

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

    But if after using Synchronized both the processes cant simultaneously access the integer and increment then why even using multi-threading then, wouldn't it take the same time as a single-threaded process ?

    • @nikilmanu334
      @nikilmanu334 20 วันที่ผ่านมา

      probably late but rest of the code in threads (before and after the method call) run simultaneously.

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

    I have query if we are making this method synchronized ( thread safe)then what is the use of using thread???? Please answer this..

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

      The 2 threads that we are using can do stuff independently. They must only wait when using our synchronized method. In this example used for learning it is useless, it's true (the whole work is only +1 ~ lol). But imagine that instead of "+1" the thread had to do some other function/method. Then both threads can work at the same time and only have to wait for each other to finish, before using our synchronized method! Let's say thread 1 counts to 10 million and the other also. Both threads can count to 10 million independently, but then they have to wait before using the final method to save their work, so no data is lost. Saving their work (the threads work) can happen very fast, but their work not.
      So for example if we have 1 thread, you need to count to 20 million, then save. With 2 threads you can only count to 10 million twice at the same time, then save ( and add the numbers together and get 20 million). You save about half the time with 2 threads, because the waiting is very small compared to the work they do.
      I hope this is more clear now :)

  • @159saur
    @159saur 4 ปีที่แล้ว

    Sir can you please make a video on thread pool and thread group.

  • @sergeyyakovlev4401
    @sergeyyakovlev4401 6 ปีที่แล้ว

    It is a very good video. It helped me a lot.

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

    if only 1 thread is working at a time then why use thread concept??

  • @unbiased-review
    @unbiased-review 6 ปีที่แล้ว +4

    Well explained sir.

  • @giorgi23
    @giorgi23 5 ปีที่แล้ว

    Thanks a lot sir. You have very good explanation

  • @belikeprogrammer5237
    @belikeprogrammer5237 8 ปีที่แล้ว

    nice explanation sir

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

    thank you, all the best!

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

    Good job sir

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

    I can't control the laugh. When both intelligent are adding the same value! :)

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

    what is the benefit of assigning priority to a thread??

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

    excellent video

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

    Damn cool explanation over this confusing topic....

  • @d-news4076
    @d-news4076 2 ปีที่แล้ว

    Thanks! it helped me!

  • @billyramogi8864
    @billyramogi8864 6 ปีที่แล้ว

    Awesome tutorials

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

    Thank you so much bro!
    it worked!

  • @sanjaypatel-ux9zb
    @sanjaypatel-ux9zb 7 ปีที่แล้ว

    nicely explained good example. thanx..

  • @anandjyoti3652
    @anandjyoti3652 6 ปีที่แล้ว

    Thank You. Nicely explained

  • @abdulqayyumisahak5190
    @abdulqayyumisahak5190 7 ปีที่แล้ว

    good explanation. Thanks for the tutorial!

  • @simim4145
    @simim4145 7 ปีที่แล้ว

    thank u soo much sir nice explanation...

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

    The ways of your explanation is awesome...😊

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

    How to sort list of string using multithreading in java?

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

    can we put the count variable synchronized instead??

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

    You are the best

  • @siddheshb.kukade4685
    @siddheshb.kukade4685 4 ปีที่แล้ว +1

    I am getting output count = 2000 without using the synchronised keyword

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

      try 10000 or even a bigger number.

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

    is there any difference if i run the same code in java 8? I did the same code without join() and i got 2000 everytime as the count

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

    what is the drawback of using synchronized .?

  • @bharathrajvarma6011
    @bharathrajvarma6011 5 ปีที่แล้ว

    very clear . thank you sir.

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

    Sir I getting an error on forloop at c what should I do .I am also updated jdk 7 to 8

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

    but sir u are giving the join method in t1 and t2 so it means t1 will work separately and also t2 then why the count is coming as weird

  • @mayurpatilify
    @mayurpatilify 6 ปีที่แล้ว

    Nice explained......

  • @mordernmonk194
    @mordernmonk194 26 วันที่ผ่านมา

    thanks sir you explain very well

  • @srivasavi4541
    @srivasavi4541 5 ปีที่แล้ว

    Sir,why didn't you use try and catch keywords when you used throws ?

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

    Even though the count is not initialised why he didn't get the error?

  • @ritwikjha156
    @ritwikjha156 5 ปีที่แล้ว

    Without synchronized the condition is in deadlock?

  • @neelancheri_yahoo
    @neelancheri_yahoo 6 ปีที่แล้ว

    Very helpful video

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

    sir how we can refer to a non-final variable inside an inner class defined in a different method

    • @hemantrai5412
      @hemantrai5412 5 ปีที่แล้ว

      Same.
      Local variable c is accessed from within inner class;needs to be declared final..

  • @hitanshimehta430
    @hitanshimehta430 5 ปีที่แล้ว

    Thank you so much!!! It helped alot!!

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

    how is join not allowing the first thread to complete and then execute the second thread, isn't that the purpose of join ?
    why do I feel you didn't use join property like start three 1 then join, then start thread 2 then join.
    or for the sake of the demo, you could have ignore join completely I believe.