Тёмный

Java Lock 

Jakob Jenkov
Подписаться 40 тыс.
Просмотров 43 тыс.
50% 1

The Java Lock interface represents a concurrent lock which can make sure only one thread at a time can lock the lock, perform its critical logic atomically, and unlock the lock again. A Java Lock is similar to a Java Synchronized block, but is more flexible and has more features.
A Java Lock can be reentrant, meaning the same thread can lock the Lock more than one time. The Lock must then be unlocked the same number of times before it is fully unlocked for other threads.
A Java Lock can guarantee fairness among threads waiting to lock the Lock, meaning the threads will be guaranteed to be allowed to lock the Lock in the same sequence the threads called the Lock.lock() method. This prevents the situation of starvation, where a waiting thread is never allowed to lock the Lock because other threads keep "passing" it in the waiting queue.
Chapters:
0:00 Java Lock and ReentrantLock introduction
2:02 Two concurrent Counter class examples
3:38 Lock and unlock the Lock inside a try-finally clause
5:00 Lock reentrance
7:03 Lock reentrance use case
10:24 Lock fairness
14:40 Lock examples
19:25 Lock.lockInterruptibly()
20:42 Lock.tryLock()
24:04 ReentrantLock methods
26:07 Java Lock vs. synchronized blocks - differences and similarities.
Java Lock Tutorial - text:
tutorials.jenkov.com/java-util...
Java Synchronized Blocks - text / video:
tutorials.jenkov.com/java-conc...
• Java Synchronized - Th...
Reentrance Lockout - text:
tutorials.jenkov.com/java-conc...
Starvation and Fairness - text:
/java-concurrency/starvation-and-fairness.html
Java Concurrency tutorial - text / video playlist:
tutorials.jenkov.com/java-conc...
• Java Concurrency and M...

Наука

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

 

25 июн 2024

Поделиться:

Ссылка:

Скачать:

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

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 107   
@sreekaransrinath4315
@sreekaransrinath4315 3 года назад
Drinking game: Every time Jakob says "lock", take a shot
@JakobJenkov
@JakobJenkov 3 года назад
😊
@michaelmedvedskiy7449
@michaelmedvedskiy7449 3 года назад
got alcohol poisoning ty
@LuneAgent
@LuneAgent 2 года назад
So if a thread locks a ReentrantLock lock, a lock is locked and will not be unlocked until the lock count of the lock is 0. And before that lock unlocks it blocks the other threads until they get their own chance to lock a lock and increase its lock counter, then they get the lock unlocked and the next thread is ready to lock the lock. Wow, it locks really good.
@jober7660
@jober7660 6 месяцев назад
I'm insanely grateful for this series, you explain everything in the best way possible!
@JakobJenkov
@JakobJenkov 5 месяцев назад
Thank you !! :-) ... and you are welcome :-)
@shubhamagarwal1434
@shubhamagarwal1434 Месяц назад
God of Concurency..i have bene flowing you since my 2014 when you used to write blog post only...by going through your post i attend interview like a LION when they ask mutithreading qns...Thanks a Lot form 10+ yrs exp guy from BLR,India.
@JakobJenkov
@JakobJenkov Месяц назад
Thanks a lot ! :-)
@29_Blocks
@29_Blocks 2 года назад
Hi, Jakob. Your videos are really useful to me and thanks a lot. Please continue!
@JakobJenkov
@JakobJenkov 2 года назад
Thank you very much! Glad my videos help you! I will continue, although I have less time than I did previously to make videos. But I will release some from time to time - like I just did today (about Thread Congestion in Java).
@ffatheranderson
@ffatheranderson 3 года назад
13:25 now I know what "starvation" means in multithreading context.
@kamelwerghi6178
@kamelwerghi6178 3 года назад
keep up the great work, you deserve to have millions of subs and views 💯
@JakobJenkov
@JakobJenkov 3 года назад
Thanks :-D ... I wish ! ;-)
@utkuceylan2309
@utkuceylan2309 Год назад
Thank you Jakob for this awesome video that enlightened the topic for me!
@JakobJenkov
@JakobJenkov Год назад
You are welcome! :-)
@amiralitavakoli1604
@amiralitavakoli1604 2 года назад
Probably the best tutorial about the Lock interface. also, I can feel semantic satiation happening for you because of the word "lock". 😁
@JakobJenkov
@JakobJenkov 2 года назад
Thanks ! ... yes, it was hard to say the word "Lock" all these times! :-D ... I remember making many mistakes during recording :-D
@LuneAgent
@LuneAgent 2 года назад
@@JakobJenkov probably it's possible to use another verb instead of "lock" to reduce this effect, maybe like: "lock and unlock the lock" -> "close and open the lock" or "occupy and unoccupy the lock" or "turn on and turn off the lock" or "capture and free the lock" I'm not really sure though, cause locking and unlocking sound the most natural for locks :)
@yonatanshkolsky4396
@yonatanshkolsky4396 2 года назад
Your tutorials are amazing!!
@JakobJenkov
@JakobJenkov 2 года назад
Thanks :-)
@smitmandavia5044
@smitmandavia5044 5 месяцев назад
Thanks again for the great detailed videos! :D The best video I could find for multithreading and concurrency. One suggestion is to also explain example use case of lockInterruptible.
@manojkrishna7693
@manojkrishna7693 3 года назад
Crystal clear explanation!
@JakobJenkov
@JakobJenkov 3 года назад
Thank you ! Great to hear! :-)
@ffatheranderson
@ffatheranderson 3 года назад
22:23 good explanation on use case for try lock.
@vdiitd
@vdiitd 3 года назад
Hello Jakob, I just found your channel today while searching for Java multi-threading tutorials. Your videos are the best ones that I have come across. I just realized that this video was posted today while I was watching your playlist on multi-threading. Timely post I guess :D. Are you going to post more videos on this topic?
@JakobJenkov
@JakobJenkov 3 года назад
How lucky 😁 Yes, I plan to still add more videos within this topic. There are several basic topics to cover, and I also want to cover various thread architectures.
@ffatheranderson
@ffatheranderson 3 года назад
Confirmed. I like the quality and detailsation of your videos and articles. Only your competitors are (I would say if you let me) Baeldung and Mkyong. 😊 PS: just realized that Baeldung is not a person 🤣...
@JakobJenkov
@JakobJenkov 3 года назад
Baeldung started out as a single person, but then he started hiring writers.
@vdiitd
@vdiitd 3 года назад
@@JakobJenkov That's awesome. Looking forward to learn more from you! 😊
@Simon-fe7ti
@Simon-fe7ti 3 года назад
I can only second that!
@aymmen6538
@aymmen6538 3 года назад
Hi Jakob, thank you for your good explanation and your efforts. Just to confirm that the two Counter implementations CounterSynchronized and CounterLock are not equivalents? In the first one, the methods inc() and getCount() can be called "simultaneously" by two threads. In the second one, the same Lock Object is used in the two methods, so they can't be called "simultaneously" by two different threads.
@JakobJenkov
@JakobJenkov 3 года назад
No - in CounterSynchronized only 1 thread can call either inc() or getCount() at a time, because both methods are synchronized (on the object instance). That is also the case with CounterLock.
@ManishKumar-qx1kh
@ManishKumar-qx1kh Месяц назад
Hi Jakob, great content as usual but i am adding a small suggestion that you should've explained about other implementations of Lock interface and how many locks are there in java etc.
@JakobJenkov
@JakobJenkov 22 дня назад
You can look that information up in the JavaDoc :-)
@technophylic3321
@technophylic3321 5 месяцев назад
Brother your content is really top notch, keep doing the good work. Thanks 👍🏻
@JakobJenkov
@JakobJenkov 4 месяца назад
Thank you ! I really appreciate your feedback! :-)
@bocumasix1010
@bocumasix1010 3 года назад
Hi Jakob. Your blog and videos are always helpful to me, in getting my job and performing well in it. Have you got a patreon or is there a way I can buy you a coffee? Keep up the great work
@JakobJenkov
@JakobJenkov 3 года назад
Thank you! Glad my tutorials are helpful! I did have a Patreon account once, but nobody every signed up (developers like free stuff ;-) ), so I shut it down again.
@av98
@av98 3 года назад
Thank you for the video.
@JakobJenkov
@JakobJenkov 3 года назад
You're welcome !!
@rahulgupta373
@rahulgupta373 6 месяцев назад
I watched various videos but this one was the best. Thanx for such great video. And keep spreading knowledge😁....
@JakobJenkov
@JakobJenkov 5 месяцев назад
You are welcome - and thank you for your kind words! :-)
@leetcodeproblems1169
@leetcodeproblems1169 Год назад
Hi Jakob, Thnx for such a great tutorial. I wonder How do you create the diagrams which you use during the lectures?
@JakobJenkov
@JakobJenkov Год назад
I use various diagram software :-)
@BJ-xg7io
@BJ-xg7io 3 года назад
Hi Jakob, the tutorials are great, in fact I can say its the best I have found so far on multithreading. One question I have is for the tryLock() example. When thread3 is in queue and waiting for 1000ms, does it need to be next in line to be able to lock the Lock or can it be in any part of the queue and will still be able to lock the Lock if the Lock is unlocked during that 1000ms time?? From what you said in the video, I think the thread has to be next in line to be able to lock the Lock but just want to confirm if I understand it correctly.
@JakobJenkov
@JakobJenkov 3 года назад
When in doubt about a detail like this, it can be a good idea to check the official JavaDoc for the given method. There you can often find such details listed. The tryLock() method without a timeout does *not* respect other threads waiting in line to lock the ReentrantLock. The tryLock(timeout) method - the version with a timeout - *does* respect other threads waiting in line to lock the Lock. See the official JavaDoc here: docs.oracle.com/en/java/javase/14/docs/api/java.base/java/util/concurrent/locks/ReentrantLock.html#tryLock(long,java.util.concurrent.TimeUnit)
@BJ-xg7io
@BJ-xg7io 3 года назад
@@JakobJenkov Thanks so much!
@Felitsius
@Felitsius Год назад
great video!
@JakobJenkov
@JakobJenkov Год назад
Thanks 😊
@19891214ful
@19891214ful 2 месяца назад
Im dizzy hearing lock 😵‍💫 Btw great video.
@JakobJenkov
@JakobJenkov 2 месяца назад
Thanks 😅
@mostafahassan6889
@mostafahassan6889 5 месяцев назад
"lock the lock" for the amazing explanation :D
@JakobJenkov
@JakobJenkov 5 месяцев назад
Yeah... then that topic is locked in 😉
@user-un7tf9oo9r
@user-un7tf9oo9r 7 месяцев назад
Thanks Jacob.
@JakobJenkov
@JakobJenkov 5 месяцев назад
You are welcome! :-)
@andrehalmoussa4077
@andrehalmoussa4077 Год назад
excellent explained
@JakobJenkov
@JakobJenkov Год назад
Thank you very much 😊
@khomo12
@khomo12 2 года назад
Great!
@JakobJenkov
@JakobJenkov 2 года назад
Thanks :-)
@SebastianBandera
@SebastianBandera 3 года назад
Muy bueno !!.
@JakobJenkov
@JakobJenkov 3 года назад
Gracias 😊
@SEASLU
@SEASLU 3 года назад
Thanks for sharing Jakob. Just wonder if you've covered Condition anywhere?
@JakobJenkov
@JakobJenkov 3 года назад
What exactly do you mean by "Condition" ?
@SEASLU
@SEASLU 3 года назад
@@JakobJenkov I meant this one docs.oracle.com/javase/7/docs/api/java/util/concurrent/locks/Condition.html
@abodalatrash1860
@abodalatrash1860 2 года назад
Amazing bro
@JakobJenkov
@JakobJenkov 2 года назад
Thanks :-D
@nickfast8624
@nickfast8624 2 года назад
Are there any difference if you put lock.lock() right before try-finally block and not within try block? For example The art of multiprocessor programming suggest the approach with acquiring lock before entering try block.
@JakobJenkov
@JakobJenkov 2 года назад
There should not really be any difference there... except if lock.lock() can throw an exception you are interested in catching somehow.
@aymmen6538
@aymmen6538 3 года назад
Another question : are there really a benefit when making the count property volatile ?
@JakobJenkov
@JakobJenkov 3 года назад
In this case - no, there is not. The access pattern (check-then-update) means that simply using a volatile variable is not enough - you need the atomicity of a synchronized block, or a critical section guarded by a Lock. However, once you have a synchronized block or a block guarded by a Lock, you no longer need the variable to be volatile - because the synchronized block / Lock guarded block provides the same visibility guarantees as a volatile variable does.
@tomasma4896
@tomasma4896 3 года назад
Is that lock needed for a getter when inc is already synchronized ?
@JakobJenkov
@JakobJenkov 3 года назад
Since reading a variable doesn't change it - synchronization / locking is not necessary to guarantee correct behaviour. However, to guarantee "visibility" of the variable that is protected by the Lock - you should consider locking and unlocking when accessing it (or make the variable itself volatile).
@COFFEEWITHJAVA
@COFFEEWITHJAVA 2 года назад
I believe usually we do not apply locks to reading the variables. As per Jacob, I learnt that making writes to variables definately will require locks to perform this kind of operation and also to make it visible what changes you are making, you need to make that variable volatile. Otherwise, there is no guarantee that your changes are shown to other threads also. Hence, usually for a method like setter makes sense to have keyword like synchronized. Correct me if I am wrong, Jacob :) .
@leetcodeproblems1169
@leetcodeproblems1169 Год назад
Hi Jakob, One Question when a thread acquires a lock then why it needs to acquire the lock again which is basically the ReEntrant lock?
@JakobJenkov
@JakobJenkov Год назад
It does not _have to_ - but in case your code does it - if the lock is reentrant, it will not cause any problems retaking the lock by the thread that already holds it.
@Manuel-oe4gv
@Manuel-oe4gv Год назад
Hi Jokob at minute 10:24 lock is called one time in the calculate method. How is it possible to execute add/subtract if the lock was already called without unlock ?
@JakobJenkov
@JakobJenkov Год назад
If a Lock is reentrant, a thread can lock the same lock more than once, and yet only one unlock() call is necessart to unlock it.
@minasalah8210
@minasalah8210 3 года назад
thank you.
@JakobJenkov
@JakobJenkov 3 года назад
You're welcome !
@redcrafterlppa303
@redcrafterlppa303 2 года назад
4:30 interesting, i didn't know that finally was stronger than return.
@user-qk4tx9jc4m
@user-qk4tx9jc4m 3 года назад
wow..... just wow....
@JakobJenkov
@JakobJenkov 3 года назад
Thanks ! :-)
@kaixunzhang4702
@kaixunzhang4702 2 года назад
Hi Jakob, thanks for the wonder explanation!! However, your website is down. Can you please check?
@JakobJenkov
@JakobJenkov 2 года назад
Hi Kaixun, thank you for notifying me! ... the server hosting tutorials.jenkov.com is getting old, and has become a bit unstable unfortunately. This is the 3rd crash within a short time period. I will be moving all the tutorials to jenkov.com soon though (a few are already moved) .
@principlesoflife172
@principlesoflife172 3 года назад
Whatever expertise.. still he can learn something from you.. I am not sure how can we repay you..
@JakobJenkov
@JakobJenkov 2 года назад
Thanks - I am glad my videos are of helpful to you :-)
@redcrafterlppa303
@redcrafterlppa303 2 года назад
3:30 the lock variable needs to be static
@JakobJenkov
@JakobJenkov 2 года назад
No it should not an instance variable because it protects that single object. Otherwise it would lock all objects of that class.
@tobias7256
@tobias7256 2 года назад
1:24 lock lock lock lock lock
@JakobJenkov
@JakobJenkov 2 года назад
... followed by unlock unlock unlock unlock ... in that exact sequence ;-)
@prometheusli101
@prometheusli101 2 года назад
is java lock equivalent to kotlin await?
@JakobJenkov
@JakobJenkov 2 года назад
I don't know Kotin well enough to be able to say, unfortunately!
@theyaghu
@theyaghu 2 года назад
I understand what you said but still I am in confusion can you explain even better. You teaching is excellent but I am not able to understand (my fault )...!!!!!
@JakobJenkov
@JakobJenkov 2 года назад
What part exactly is it you still need some help with?
@zombiestrange6475
@zombiestrange6475 Год назад
Lock lock lockalock lokulokulock
@JakobJenkov
@JakobJenkov Год назад
;-)
@klajdi9656
@klajdi9656 Год назад
take a shot every time it says lock
@JakobJenkov
@JakobJenkov Год назад
It better be a shot of something healthy then ;-)
Далее
Java ExecutorService - Part 1
20:55
Просмотров 51 тыс.
Olive can see you 😱
01:00
Просмотров 5 млн
LISA - ROCKSTAR (MV Teaser)
00:10
Просмотров 2 млн
Compare and Swap in Java
24:21
Просмотров 15 тыс.
The Java Memory Model - The Basics
23:41
Просмотров 119 тыс.
False Sharing in Java
17:33
Просмотров 11 тыс.
Deadlock in Java
17:48
Просмотров 21 тыс.
Thread Pools in Java
18:04
Просмотров 63 тыс.
Thread Signaling in Java
23:26
Просмотров 10 тыс.
Java ReentrantLock - fairness, tryLock and more
13:17
Просмотров 130 тыс.
Java Volatile
21:26
Просмотров 43 тыс.
A Practical Example How To Use Interfaces In Golang
14:42