13.7 Multithreading Synchronized Keyword

Поділитися
Вставка
  • Опубліковано 16 гру 2024

КОМЕНТАРІ • 230

  • @avahome5285
    @avahome5285 3 роки тому +86

    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 9 місяців тому +3

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

    • @redwanniloy2068
      @redwanniloy2068 8 місяців тому

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

  • @kartzoftvasu3162
    @kartzoftvasu3162 4 роки тому +96

    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 6 місяців тому

      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 6 місяців тому

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

  • @FedoraTime
    @FedoraTime 5 років тому +67

    Very clear and simple example, thank you!

  • @uditswaroopa5809
    @uditswaroopa5809 4 роки тому +35

    you cleared my doubt I did not skip any ad! Modern Guru Dakshina (and I learned If we don't skip first ad YT shows a second one)

  • @rajag8851
    @rajag8851 6 років тому +3

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

  • @mohammedhammad2113
    @mohammedhammad2113 3 роки тому +10

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

  • @rajeshjain1204
    @rajeshjain1204 9 місяців тому

    Best Java Teacher in UA-cam 💯

  • @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.

  • @shreyash8813
    @shreyash8813 3 роки тому +2

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

  • @dharmikbatra42
    @dharmikbatra42 4 роки тому +1

    Bucky Roberts from India .
    Amazing tutorials sir!

  • @420thlegioner8
    @420thlegioner8 4 роки тому +1

    Godlike explanation, CLEAR AND SIMPLE !

  • @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 3 місяці тому

      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.

  • @IndukuriMaheswarReddy
    @IndukuriMaheswarReddy 2 роки тому +2

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

  • @sangavia6687
    @sangavia6687 2 роки тому

    Crisp and clear explanation!!

  • @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

  • @PranathNaik
    @PranathNaik 4 роки тому

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

  • @cookwithme9623
    @cookwithme9623 5 років тому +5

    @Telusko
    We need teachers like you in college : )

  • @lifeofme3172
    @lifeofme3172 Рік тому

    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.

  • @saisrisai9649
    @saisrisai9649 5 років тому +3

    thank u so much sir . such a beautiful explanation

  • @darshankhatri7375
    @darshankhatri7375 5 років тому

    Sir you are Legend for CS Students.

  • @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).

  • @gfunkonthecheesetree
    @gfunkonthecheesetree Рік тому

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

  • @ankitgoyal34
    @ankitgoyal34 5 років тому +1

    Excellent video, makes understanding very simple and easy!!

  • @pratiklakade1062
    @pratiklakade1062 7 місяців тому

    What an explaination sir .Just Wow.

  • @shankerbabumothkuri3225
    @shankerbabumothkuri3225 4 роки тому

    Naveen,
    You are an awesome teacher!!

  • @ashwanikumar4288
    @ashwanikumar4288 6 років тому +1

    explanation was crystal clear. Thanks a ton!

  • @mohammadarshad2908
    @mohammadarshad2908 5 років тому +1

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

  • @nynra6584
    @nynra6584 8 років тому +1

    Thank you for explaining it comprehensively.

  • @maheshmahadar4780
    @maheshmahadar4780 3 роки тому

    awesome explanation with example

  • @ferfykins
    @ferfykins 5 років тому +3

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

  • @sravanisunkara3093
    @sravanisunkara3093 4 роки тому

    wow! crystal clear explanation.THANK YOU!

  • @anelemadonda6191
    @anelemadonda6191 3 роки тому

    best channel ever

  • @vandadvafai48
    @vandadvafai48 2 роки тому

    You saved me so much time...

  • @davidlira8853
    @davidlira8853 3 роки тому

    Great example brother

  • @aniruddhachunne3011
    @aniruddhachunne3011 2 роки тому

    Awesomeee.... so concise and perfect explanation

  • @jananiramesh5471
    @jananiramesh5471 2 роки тому

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

  • @brucebane7401
    @brucebane7401 4 роки тому

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

  • @sf2759
    @sf2759 7 років тому +1

    Crystal clear!!!much appreciated!

  • @sairajdas6692
    @sairajdas6692 6 років тому

    Excellent explanation sir

  • @robertsedgewick1266
    @robertsedgewick1266 3 роки тому

    Brilliant explanation, thanks for sharing!

  • @isirasamarasinghe748
    @isirasamarasinghe748 2 роки тому

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

  • @karthick2237
    @karthick2237 5 років тому

    Top class teaching ..👏👏👍

  • @mohammedmoues5237
    @mohammedmoues5237 3 роки тому +1

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

  • @satishkrajendran8703
    @satishkrajendran8703 7 років тому

    Its really great. kudos to navin reddy

  • @raghulg6155
    @raghulg6155 5 років тому +1

    Damn cool explanation over this confusing topic....

  • @unbiased-review
    @unbiased-review 6 років тому +4

    Well explained sir.

  • @smithcodes1243
    @smithcodes1243 4 роки тому +2

    Ahhhh Bossss! What an explanation!

  • @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 ?

  • @ahmeedev
    @ahmeedev 3 роки тому

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

  • @axdayrboom3284
    @axdayrboom3284 6 років тому

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

  • @01234567899970
    @01234567899970 2 роки тому

    thankyou so much for great explaination

  • @giorgi23
    @giorgi23 5 років тому

    Thanks a lot sir. You have very good explanation

  • @krishnamoorthysridhar8536
    @krishnamoorthysridhar8536 Рік тому

    Good Explanation.. Thanks

  • @lakshmanan932
    @lakshmanan932 3 роки тому

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

  • @aravind1578
    @aravind1578 3 роки тому

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

  • @bekjanomirzak224
    @bekjanomirzak224 3 роки тому

    Thanks! great explanation on this topic!

  • @sedittak
    @sedittak 5 років тому

    Great video, nice explanation

  • @pratimadaswante895
    @pratimadaswante895 5 років тому +1

    The ways of your explanation is awesome...😊

  • @belikeprogrammer5237
    @belikeprogrammer5237 8 років тому

    nice explanation sir

  • @chandu.l
    @chandu.l 7 місяців тому

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

  • @titany679
    @titany679 4 роки тому

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

  • @abdulqayyumisahak5190
    @abdulqayyumisahak5190 7 років тому

    good explanation. Thanks for the tutorial!

  • @sanjaypatel-ux9zb
    @sanjaypatel-ux9zb 7 років тому

    nicely explained good example. thanx..

  • @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 3 місяці тому

      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.

  • @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.

  • @anandjyoti3652
    @anandjyoti3652 6 років тому

    Thank You. Nicely explained

  • @klemn0291
    @klemn0291 Рік тому

    excellent video

  • @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 :)

  • @billyramogi8864
    @billyramogi8864 6 років тому

    Awesome tutorials

  • @simim4145
    @simim4145 7 років тому

    thank u soo much sir nice explanation...

  • @sergeyyakovlev4401
    @sergeyyakovlev4401 6 років тому

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

  • @kacpermakuch7855
    @kacpermakuch7855 6 років тому

    I just love this intro !

  • @bharathrajvarma6011
    @bharathrajvarma6011 5 років тому

    very clear . thank you sir.

  • @baby0nboard-369
    @baby0nboard-369 3 роки тому

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

  • @ahmeedev
    @ahmeedev 3 роки тому

    Good job sir

  • @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...

  • @mayurpatilify
    @mayurpatilify 6 років тому

    Nice explained......

  • @bhaskar1843
    @bhaskar1843 7 місяців тому

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

  • @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?

  • @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 4 роки тому

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

  • @neelancheri_yahoo
    @neelancheri_yahoo 5 років тому

    Very helpful video

  • @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

  • @jakubsz4602
    @jakubsz4602 3 роки тому

    thank you, all the best!

  • @d-news4076
    @d-news4076 2 роки тому

    Thanks! it helped me!

  • @bidirectional3619
    @bidirectional3619 6 років тому +1

    package com.advance;
    class counter implements Runnable {
    int count = 0;
    public void incriment() {
    count++;
    }
    public void run(){
    counter obj=new counter();
    obj.incriment();
    }
    }
    public class SyncDemo {
    public static void main(String[] args) throws Exception {
    // write your code here
    counter obj1 = new counter();
    Thread t1=new Thread(obj1);
    for(int i=1;i

  • @CoderSV3
    @CoderSV3 4 роки тому

    Thank you very much.
    Now I understand!

  • @anikeatbansal6916
    @anikeatbansal6916 7 років тому +1

    why we are using throws exception with join() ?
    what if i use Thread.sleep() after t1.start() in place of synchronized?

    • @rup7591
      @rup7591 7 років тому +3

      anikeat bansal
      1)Throws keyword is used to declare an exception.Exception can be handled by the calling method or it can be catches by that method.
      2)It would work with this example.Because the time taken by the machine to perform this operation is so tiny.But how can you definitey tell how much a function will take to execute .If you wait any less than that you are running into trouble of thread collision and if u wait more..you are wasting time...And it also create hassle for the user/programmer who will call the function .
      However u can call thread.sleep() inside of the synchronized function but that will not open the lock when one thread is at sleep.
      There is a separate function called obj.wait() which an be used for that purpose .

  • @srikanthkolipaka2103
    @srikanthkolipaka2103 6 років тому

    Pretty clear 👍🏿👍🏿

  • @whimsicalvibes
    @whimsicalvibes 6 років тому

    by using join() on t1 and t2 are not we stopping main to access the increment method before the two finish? Is Synchronized still required?

  • @VaishaliGoyal49
    @VaishaliGoyal49 6 років тому

    Well explained

  • @159saur
    @159saur 4 роки тому

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

  • @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

  • @103rahul9
    @103rahul9 4 роки тому +1

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

  • @iamtechboy3298
    @iamtechboy3298 7 років тому

    Thankss u made it much easy..

  • @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

  • @hitanshimehta430
    @hitanshimehta430 5 років тому

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

  • @rohithkumar7821
    @rohithkumar7821 4 роки тому

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

  • @vivekmishra1520
    @vivekmishra1520 3 роки тому

    thanks you for explain the concept very Cleary. I have one doubt , can we add volatile keyword to the count variable also and will it work simillar, If not can you please explain why?

  • @vishalramoju5432
    @vishalramoju5432 4 роки тому

    can we put the count variable synchronized instead??