Advanced Java: Multi-threading Part 11 - Deadlock

preview_player
Показать описание
-------------------------------------------------------------------------------------------------------------------------------------------
The causes of deadlock and two things you can do about it. This video also covers how to write a method that can safely acquire any number of locks in any order without causing deadlock, using the tryLock() method of ReentrantLock.
--------------------------------------------------------------------------------------------------------------------------------------------
Рекомендации по теме
Комментарии
Автор

Man these tutorials are very good, way better than my profs :P

uNbuNdd
Автор

There's no physical connection; it's just that you always acquire lock1 before dealing with acc1, and lock2 for acc2. By sticking to that convention, you ensure that you never have two threads modifying an account at the same time. In a larger system, you could have a table of accounts vs. locks or something.

caveofprogramming
Автор

The locks don't actually lock objects as such; they are just a way of coordinating threads. We choose a lock for some data, then decide that no thread can alter that data unless it first acquires the lock.

caveofprogramming
Автор

I never really understood deadlocks very well before..This tutorials has clarified all my doubts and made my understanding super clear.Thank you John

deeptiaslekar
Автор

That would work here. The problem with using one lock would only occur if you had lots of accounts and lots of threads. Then you need to lock both accounts, out of the many possible accounts.

caveofprogramming
Автор

The problem is that you now have to query all such singleton objects to find out if any of them lock either of the accounts that you want to deal with. When you are deciding whether you can allow the creation of an object for a particular pair of accounts, you now have a lengthy query to carry out. By the time you reach a decision, another thread might have created an object that locks one of your accounts. So you end up with a huge table which can only be accessed by one thread at a time.

caveofprogramming
Автор

Small programs like this will often seem to work fine even with no locks ... But you can't rely on it working unless you lock all resources that are shared between threads. In this program you could use one lock to cover both accounts, but if you had many accounts and didn't know which two you were going to lock each time, and you had many threads accessing them, you'd need to have one lock per account.

caveofprogramming
Автор

Great tutorial.. Provides the clear understanding of locks.... Excellent Work :)

aspdeepakyt
Автор

Go to my website, Cave of Programming, scroll down to the YouTube videos section, then click on an article there. The source is embedded in the page.

caveofprogramming
Автор

simply

a must watch video in this series

mynameraja
Автор

The docs say that this will not acquire the lock if the thread has been interrupted. I have a video on Interrupted Exceptions which explains interrupting threads.

caveofprogramming
Автор

if you hold the Alt key you can move lines by pressing the up and down keys, as well as if you hold the Alt + Crtl you can copy lines by pressing the arrow keys

Simorenarium
Автор

Hello, before all Thanks for your tutorials are really interesting and useful. With the respect to this video you told that lock1 and lock2 are connected to the acc1 and acc2 but where is explicated this relationship?
I understand that in this case the use of 2 locks are used to show the deadlock situation but if I run try the code using only one lock object and it works fine.
Why do we need two locks? how can we bind the locks to the accounts?
Thanks in Advance

diegomarcheselli
Автор

This is indeed helpful, specially for interviews!

chiranjibghorai
Автор

Thank -you. :-) It makes sense now. So the only using of "Reentrant lock" Vs Synchronized block is that Reentrant Locks are elegant to use and they have now provision of "trying" the lock which returns immediately, where from we can decide the proper actions and hence avoiding deadlocks.

Just curious to know, where are you from ?
Heartfelt Thanks from INDIA !!! Cheers!!! :-)

sahilrally
Автор

Took me a while to wrap my head around the acquirelocks method, but I understand it! I was bashful at first in accepting the fact that the <boolean object>. trylock () method from the Lock class did TWO things, not only does give a boolean value of true IF it finds that the thread calling the method HAS the lock, but it also in itself actually tells the thread to actually try and acquire the lock..

bluex
Автор

7:02
*Thread1* : you first give me lock2, then I give you lock1.
*Thread2* : no you first give me lock1, then I give you lock2.



Just like kids fighting for exchanging things.

eboubaker
Автор

Hi John, thanks for your nice video. How lock1 is related with acc1 and lock2 with acc2? How does the system knows about that?

mmmm
Автор

Yeah exactly. It would work fine for this little demo, but you couldn't do it if there were lots of locks and processes.

caveofprogramming
Автор

I think I have figured this out. Please correct me if I am wrong. So in real world, there would be > 1 thread trying to manipulate a single account, in that case you would need to acquire a lock for that account. In this example you have just used 2 standalone locks, but again, practically, there will be a lock associated with each account number, and you would retrieve a lock for that account and lock it.

hemantchanchlani
visit shbcf.ru