Advanced Java: Multi-threading Part 10 - Re-entrant Locks

Поделиться
HTML-код
  • Опубликовано: 18 сен 2024
  • More courses! Free C++ course: www.udemy.com/...
    Make a social network with Spring Boot: www.udemy.com/...
    Learn about MySQL and relational databases: www.udemy.com/...
    Java desktop programming: www.udemy.com/...
    Intermediate-Advanced C++ 11: www.udemy.com/...
    -------------------------------------------------------------------------------------------------------------------------------------------
    How to use the ReentrantLock class in Java as an alternative to synchronized code blocks. ReentrantLocks let you do all the stuff that you can do with synchronized, wait and notify, plus some more stuff besides that may come in handy from time to time.
    --------------------------------------------------------------------------------------------------------------------------------------------
    More videos on my RUclips channel: / caveofprogramming

Комментарии • 68

  • @RahulSinghrs19
    @RahulSinghrs19 4 года назад +13

    John, I am revisiting these videos after 7 years because I am prepping for an interview. There is still that calmness, smoothness and ease of explaining complex topics simplistically. There are not many teachers like that, you are definitely one of the best. Cheers mate, keep up the good work.

    • @chandanpanjwani
      @chandanpanjwani 3 года назад +1

      me too can you tell me what all topics should I do after reentrant locks?

  • @vadimosm
    @vadimosm 9 лет назад +31

    One of the best tutorials about threading I've ever seen. Greetings from Antarctica.

    • @ToastyMeadows
      @ToastyMeadows 6 лет назад +4

      So you are one of those couple dozen people living in Antarctica? Are you a full resident or "summer only"?

  • @rupamroy3931
    @rupamroy3931 6 лет назад +8

    So calm and smooth, I feel like I'm walking on the beach

  • @NMOwen
    @NMOwen 10 лет назад +4

    Have commented before, elsewhere, but feel compelled to do so again, because to my mind there really is not enough I can say by way of 'thanks' for all this work you've put in to sharing your expertise. I work on Java every morning, hoping to make the career move to programmer one day (though it seems a long ways away), and your videos are a regular part of my routine. It's not just the content, it's the style that wins. It would hard take in this much info from an SME if they were not so 'listenable'. As a former teacher myself I truly appreciate your efforts.

    • @judelabeh8119
      @judelabeh8119 Год назад

      *From the future* - Are you a java programmer now?

  • @nataliemsp
    @nataliemsp 11 лет назад

    You have a fantastic, lucid way of explaining things. I have an exam tomorrow, these videos did a better job at making things clear than my entire shelf of Java books. That and the gorgeous Northern accent

  • @omairbhore
    @omairbhore 7 лет назад

    One of the best tutorials about threading I've ever seen. Greetings from Winterfell.

  • @caveofprogramming
    @caveofprogramming  11 лет назад

    You're quite right actually; I could have put the lock and unlock in increment(). Here it would have been better. There would be no side-effect for this code. I just wrote it the way I did because I thought it better illustrated the point I was making at the time.

  • @theamazingmonkey100
    @theamazingmonkey100 4 года назад +9

    great tutorial! one suggestion though, maybe edit out all the saliva sounds, they can be mildly distracting. Otherwise genuinely GREAT tutorial.

  • @danielpapukchiev3754
    @danielpapukchiev3754 7 лет назад

    Thanks for the great tutorial! I was so happy when I heard your perfect English accent as opposed to all the other Indian videos, thanks for that too.

  • @sbylk99
    @sbylk99 5 лет назад +1

    After thread2 call condition.signal(), thread 1's await () acquires the lock. So from this moment, which increment is executed firstly, 1 or 2?

  • @sahilrally4491
    @sahilrally4491 11 лет назад +1

    Thank-You. Nice Explanation. I wonder why Reentrant locks exists at first place, but eventually u said @ last that we can try as well that could not be done with synchronized blocks.

  • @starriet
    @starriet 3 года назад

    the great teacher came out of a cave!! lovely lecture!!

  • @anirudhsharma3168
    @anirudhsharma3168 2 года назад +2

    Me imagining doing 100 lines of code in few minutes:
    *Reality:* 4:46
    Also, great video. Watching this in 2021 and it still is one of the best videos for learning threading. Greetings from India.

  • @Joske369
    @Joske369 11 лет назад

    thank you for the quick response! got my exams in a week, this video helped alot :)

  • @rakesh2904
    @rakesh2904 11 лет назад +1

    Thanks for the videos these are very informative and nicely deliverd. I have one query about the implementation of lock.lock() and lock.unlock().In the code base it's been used in both the method (firstThread and secondThread) ,instead of that can we use this locking and unlocking only in increment method. please verify and do inform if there is any side effect in this approach.which one would be more appropriate from performance perspective.

  • @Nisa-yq9so
    @Nisa-yq9so 8 лет назад +1

    Thank you so much!! you can't imagine how much this video helped me!

  • @AnjaliChadha
    @AnjaliChadha 9 лет назад +2

    Why do we need to put lock.wait()/unwait() separately in firstThread() and secondThread() ?
    I think adding lock.wait/unwait() in increment() method will be enough.
    I tested by implementing both scenarios, and the behaviour seems fine.

    • @johnpurcell827
      @johnpurcell827 9 лет назад +1

      Anjali Chadha You mean await() :) There is no unwait. I'm on a limited Internet connection at the moment, so haven't watched the video to remind myself what's in it, but if I refer to the source code here: www.caveofprogramming.com/java-multithreading/java-multithreading-reentrantlock-video-tutorial-part-10.html there is no await() in secondThread(). The second thread just wakes up the first thread.

  • @caveofprogramming
    @caveofprogramming  11 лет назад

    It re-acquires it automatically. await() is guaranteed not to return until the first thread can successfully re-acquire the lock.

  • @shiweiwong5292
    @shiweiwong5292 5 лет назад

    After signaling we must call the unlock method, the other thread in awaiting state could get the lock again and move forward. thank a lot.

  • @caveofprogramming
    @caveofprogramming  11 лет назад

    I don't know. Couldn't you only lock when you want to write, and not when you read? But you have to be careful that you don't read an inconsistent state.

  • @jackbenimble99999
    @jackbenimble99999 6 лет назад

    Great tutorials - thank you! Using Blocking queues, I was able to simplify an old homework app which created a thread pool for a bank account from complex, hard-to-understand code to a trivial producer-consumer app as demonstrated in tutorial 7.
    Regarding the question of how many locks to use; in this case, since Account is in-memory and not persisted (which is probably not a real-world scenario), I think it makes more sense to synchronize on the Account object's lock. This is a good demonstration of what lock does, but it also shows that it doesn't work in the same way as synchronized since you can't lock a specific object. What do you think?

  • @jljtube
    @jljtube 10 лет назад

    Really good - just what I required explained to the point - Thanks.

  • @canastrao
    @canastrao 7 лет назад

    You help me a lot with your tutorial. Thank you very much.

  • @StriveforGainz
    @StriveforGainz Год назад

    Thanks for the explanation!

  • @aravindkatte
    @aravindkatte 5 лет назад

    Absolutely awesome videos on java multithreading concepts. I have finished almost all videos.
    May be my question looks silly, but I need clarification.
    None of your class didn't extends Thread class or didn't implements Runnable implements, then how the threads are running, is this because of the Java 8 Runnable functional interface.

    • @Email1212xxpl
      @Email1212xxpl 5 лет назад +1

      It works, because he used Thread class, to create new Object. That Object implements Runnable interface, where body of run() method is defined within creating that object itself.

  • @LoneStarVII
    @LoneStarVII 9 лет назад +2

    When you use use wait or await, when the other thread notifies or signals does the initial thread regain the lock or is it then waiting for the second thread to release the lock?

    • @johnpurcell827
      @johnpurcell827 9 лет назад +4

      Both; when you use notify, the waiting thread "wakes up", but it can't actually get back the lock until the first thread releases it. As soon as it does, it gets the lock and continues past the wait statement.

  • @Joske369
    @Joske369 11 лет назад

    you say that you have to unlock in the finally block when you signal, but when the first thread awakes, how does it get it's lock back then?

  • @eatalotoffood1635
    @eatalotoffood1635 5 лет назад

    Why USE this if we have blockingqueue? Can blockingqueue be used instead

  • @miscelaneasdealguem
    @miscelaneasdealguem 5 лет назад

    Thank you for providing those tutorials sir

  • @MrShandymilo
    @MrShandymilo 6 лет назад +2

    Total count of 3 incorrect { at 4:45 had me screaming :D

    • @duartevilar
      @duartevilar 4 года назад

      oh man me too 1yr apart, same trigger

  • @spucmu
    @spucmu 12 лет назад

    Awesome work man!! This is wonderful

  • @pinepinecone2918
    @pinepinecone2918 10 лет назад

    U are awesome ... crystal clear .. love ur accent

  • @GeneralBowden
    @GeneralBowden 11 лет назад

    Do you know how to use ReentrantReadWriteLock to allow multiple threads to read a collection, but when one thread is writing, no other threads can read or write. Thanks :)

  • @TheSridharraj
    @TheSridharraj 10 лет назад

    Hi, Could you please provide the real time usages of ReentrantLock.. real time applications please?

  • @mynameraja1989
    @mynameraja1989 12 лет назад

    another great video sir......

  • @ashwinmurali4345
    @ashwinmurali4345 9 лет назад

    Really nice tutorial!

  • @mikesca1980
    @mikesca1980 12 лет назад

    Great channel keep up with your great work :)

  • @rupaldesai7098
    @rupaldesai7098 6 лет назад

    Part 10 why is the object of the Runner class made final in App Class?

  • @rakesh2904
    @rakesh2904 11 лет назад

    Thanks for reply and clarification...!

  • @jaiadwani3305
    @jaiadwani3305 10 лет назад

    thanks :) very clear explanation.

  • @desigeekchishi
    @desigeekchishi 7 лет назад

    I run te same program but i never get anything othher tan 20k, but i added 3 more zeros and it showed different number less frewuently and higher the number it sows the same number less frequently.

  • @johnpurcell827
    @johnpurcell827 11 лет назад

    Good luck!!

  • @songleishiyulin
    @songleishiyulin 7 лет назад

    This is a excellent!

  • @vishaljagdale9412
    @vishaljagdale9412 3 года назад

    Thank you!

  • @sujeetyadav2391
    @sujeetyadav2391 9 лет назад

    Hi,
    I was using this code but not getting expected output. Can you please help me with this?
    There are two files 1) Processor.java 2) App.java
    Processor.java
    package com.sujeettech.reenterantloack;
    public class Processor {
    private int count = 0;
    private Object lock1 = new Object();
    private Object lock2 = new Object();
    void increment(){
    count++;
    }
    void firstThread(){
    synchronized(lock1){
    for(int i = 0; i

    • @dshrikanth3165
      @dshrikanth3165 6 лет назад

      Problem is that you are using 2 different object locks each thread having its own lock and proceeding with the execution in this case you shall not able to achieve mutual synchronization on same lock, may be you can use some thing like this,
      class Processor {
      private int count = 0;
      private Object lock1 = new Object();
      private Object lock2 = new Object();
      void increment(){
      //synchronized(lock2){
      count++;
      //}
      }
      void firstThread(){
      synchronized(lock1){
      for(int i = 0; i

  • @valeryantimenkov9111
    @valeryantimenkov9111 Год назад

    Why Do we need two locks?

  • @LFSDK
    @LFSDK 10 лет назад

    Sounds like this is how a sonic screwdriver works.

  • @crashoverride9681
    @crashoverride9681 7 лет назад

    Thank you bro!

  • @Sintaxx2
    @Sintaxx2 10 лет назад +1

    Wow, u bent Nederlands? U heeft dan een super goed Engels accent! :p

    • @caveofprogramming
      @caveofprogramming  10 лет назад +1

      Nee, ik ben Engels, mar ik heb twee jar in Nederlands woonde (that's probably all wrong :) )

    • @Sintaxx2
      @Sintaxx2 10 лет назад

      Cave of Programming Yes, it is a little (Nee, ik ben Engels, maar ik heb twee jaar in Nederland gewoond*) but it was clear what you were trying to say :p thanks for the tutorials by the way!

  • @joachimdietl6737
    @joachimdietl6737 2 года назад

    It must be cold in Antartica

  • @Marinakulichok
    @Marinakulichok 7 лет назад

    Please, someone explain me the role the join method here. If I run my programm this way
    th1.join();
    //th2.join();
    The count will be 2000
    but if
    //th1.join();
    th2.join(); this way the result will be 1000
    Why?????

    • @dshrikanth3165
      @dshrikanth3165 6 лет назад

      Reason being u see difference in output is that, thread1 shall start incrementing the count variable only once thread2 completes, as it goes to await state statement "condition.await();" and waits for signal method to be called and lock to be released by thread2, by the time these happens thread2 already makes count value to 10000 and in finally block it releases lock, post this thread1 gets the lock back and calls increment method continues incrementing and end result of count becomes 20000.

  • @bakrgroningen
    @bakrgroningen 11 лет назад

    Ok! this is rally weird.. because even without using the Lock. I get 2000! whats happening?

  • @Schnep
    @Schnep 11 лет назад

    Funny how your pc is set in dutch while you have a perfect english pronounciation

  • @dhirajctx
    @dhirajctx Год назад

    Is it still relevant ??

  • @kartik_adhia
    @kartik_adhia 7 лет назад

    the pop up message seemed like dutch to me..

  • @sahilrally4491
    @sahilrally4491 8 лет назад

    I Love you !