Тёмный

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

Cave of Programming
Подписаться 108 тыс.
Просмотров 123 тыс.
50% 1

More courses! Free C++ course: www.udemy.com/course/free-lea...
Make a social network with Spring Boot: www.udemy.com/course/java-spr...
Learn about MySQL and relational databases: www.udemy.com/course/mysql-an...
Java desktop programming: www.udemy.com/course/java-swi...
Intermediate-Advanced C++ 11: www.udemy.com/course/learn-ad...
-------------------------------------------------------------------------------------------------------------------------------------------
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 RU-vid channel: / caveofprogramming

Наука

Опубликовано:

 

30 янв 2012

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 68   
@RahulSinghrs19
@RahulSinghrs19 4 года назад
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 2 года назад
me too can you tell me what all topics should I do after reentrant locks?
@rupamroy3931
@rupamroy3931 5 лет назад
So calm and smooth, I feel like I'm walking on the beach
@vadimosm
@vadimosm 9 лет назад
One of the best tutorials about threading I've ever seen. Greetings from Antarctica.
@ToastyMeadows
@ToastyMeadows 6 лет назад
So you are one of those couple dozen people living in Antarctica? Are you a full resident or "summer only"?
@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
@anirudhsharma3168
@anirudhsharma3168 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.
@NMOwen
@NMOwen 9 лет назад
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?
@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.
@starriet
@starriet 3 года назад
the great teacher came out of a cave!! lovely lecture!!
@Nisa-yq9so
@Nisa-yq9so 8 лет назад
Thank you so much!! you can't imagine how much this video helped me!
@StriveforGainz
@StriveforGainz Год назад
Thanks for the explanation!
@jljtube
@jljtube 10 лет назад
Really good - just what I required explained to the point - Thanks.
@Joske369
@Joske369 11 лет назад
thank you for the quick response! got my exams in a week, this video helped alot :)
@spucmu
@spucmu 11 лет назад
Awesome work man!! This is wonderful
@canastrao
@canastrao 7 лет назад
You help me a lot with your tutorial. Thank you very much.
@theamazingmonkey100
@theamazingmonkey100 4 года назад
great tutorial! one suggestion though, maybe edit out all the saliva sounds, they can be mildly distracting. Otherwise genuinely GREAT tutorial.
@ashwinmurali4345
@ashwinmurali4345 9 лет назад
Really nice tutorial!
@jaiadwani3305
@jaiadwani3305 10 лет назад
thanks :) very clear explanation.
@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.
@miscelaneasdealguem
@miscelaneasdealguem 4 года назад
Thank you for providing those tutorials sir
@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?
@mikesca1980
@mikesca1980 12 лет назад
Great channel keep up with your great work :)
@rakesh2904
@rakesh2904 11 лет назад
Thanks for reply and clarification...!
@mynameraja1989
@mynameraja1989 12 лет назад
another great video sir......
@sahilrally4491
@sahilrally4491 11 лет назад
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.
@sbylk99
@sbylk99 5 лет назад
After thread2 call condition.signal(), thread 1's await () acquires the lock. So from this moment, which increment is executed firstly, 1 or 2?
@songleishiyulin
@songleishiyulin 7 лет назад
This is a excellent!
@pinepinecone2918
@pinepinecone2918 10 лет назад
U are awesome ... crystal clear .. love ur accent
@vishaljagdale9412
@vishaljagdale9412 2 года назад
Thank you!
@LoneStarVII
@LoneStarVII 9 лет назад
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 лет назад
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.
@crashoverride9681
@crashoverride9681 7 лет назад
Thank you bro!
@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.
@johnpurcell827
@johnpurcell827 11 лет назад
Good luck!!
@caveofprogramming
@caveofprogramming 11 лет назад
It re-acquires it automatically. await() is guaranteed not to return until the first thread can successfully re-acquire the lock.
@rakesh2904
@rakesh2904 11 лет назад
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.
@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?
@TheSridharraj
@TheSridharraj 9 лет назад
Hi, Could you please provide the real time usages of ReentrantLock.. real time applications please?
@AnjaliChadha
@AnjaliChadha 9 лет назад
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 лет назад
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.
@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 :)
@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 лет назад
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.
@eatalotoffood1635
@eatalotoffood1635 5 лет назад
Why USE this if we have blockingqueue? Can blockingqueue be used instead
@rupaldesai7098
@rupaldesai7098 5 лет назад
Part 10 why is the object of the Runner class made final in App Class?
@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.
@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.
@LFSDK
@LFSDK 10 лет назад
Sounds like this is how a sonic screwdriver works.
@valeryantimenkov9111
@valeryantimenkov9111 9 месяцев назад
Why Do we need two locks?
@MrShandymilo
@MrShandymilo 5 лет назад
Total count of 3 incorrect { at 4:45 had me screaming :D
@duartevilar
@duartevilar 4 года назад
oh man me too 1yr apart, same trigger
@sahilrally4491
@sahilrally4491 8 лет назад
I Love you !
@Sintaxx2
@Sintaxx2 10 лет назад
Wow, u bent Nederlands? U heeft dan een super goed Engels accent! :p
@caveofprogramming
@caveofprogramming 10 лет назад
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!
@Schnep
@Schnep 11 лет назад
Funny how your pc is set in dutch while you have a perfect english pronounciation
@joachimdietl6737
@joachimdietl6737 2 года назад
It must be cold in Antartica
@sujeetyadav2391
@sujeetyadav2391 8 лет назад
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
@bakrgroningen
@bakrgroningen 10 лет назад
Ok! this is rally weird.. because even without using the Lock. I get 2000! whats happening?
@dhirajctx
@dhirajctx Год назад
Is it still relevant ??
@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.
@kartik_adhia
@kartik_adhia 7 лет назад
the pop up message seemed like dutch to me..
@omairbhore
@omairbhore 6 лет назад
One of the best tutorials about threading I've ever seen. Greetings from Winterfell.
Далее
Advanced Java: Multi-threading Part 11 - Deadlock
14:40
Ai + Photoshop Manipulation Part 1
15:09
Просмотров 35
Java Lock
28:51
Просмотров 43 тыс.
Java ReentrantLock - fairness, tryLock and more
13:17
Просмотров 130 тыс.
Advanced Java: Multi-threading Part 12 - Semaphores
10:56
Big Update in Java for Learners and Trainers
0:59
Просмотров 325 тыс.
Deadlock - Concurrency #1 - Advanced Java
35:59
Просмотров 124 тыс.