- Tubelator AI
- >
- Videos
- >
- Education
- >
- 33. Lock-Free Concurrency | Compare-and-Swap | Atomic & Volatile Variables | Multithreading Part5
33. Lock-Free Concurrency | Compare-and-Swap | Atomic & Volatile Variables | Multithreading Part5
➡️ Notes link: Shared in the Member Community Post (If you are Member of this channel, then pls check the Member community post, i have shared the Notes link there)
➡️ Join this channel to get access to member only perks:
https://www.youtube.com/channel/UCDJ2HAZ_hW-DMJj_U0zN38w/join
Chapters:
00:00 - Understanding CAS (Compare and Swap)
15:06 - Atomic Variables
32:48 - Volatile Variables
38:53 - Concurrent Collection
#java #softwareengineer #multithreading
Video Summary & Chapters
No chapters for this video generated yet.
Video Transcript
Hey guys, welcome to concept encoding and this is Shrayansh and today in multi threading
We are going to cover this two topics atomic variables
And also we will see the difference with the volatile and also the concurrent collections
We have already discussed concurrent collection during the collections topics when we were discussing
Right, but we will have a quick look at it
So let's start without wasting any time
So, here if an interview, if anybody asked you, hey, in how many ways you can achieve
the concurrency?
What would your answer would be?
So there are two ways in which we can achieve the concurrency one is the lock based mechanism
Another is lock free mechanism
So lock based we have already seen so many different ways synchronized
Rent-rent, stamp, read-write, semaphores all this we have seen already
But there is another way lock free mechanism
Right in this we have put locks
right to make sure that only one thread
can be used at very different scenarios where we have lot of complexity, where we
have lot of business logics. This will comes into the picture. Lock-free
mechanism is used very specific area, very very specific use case I would say
that. Okay I will tell you about that use case slowly when we will go into the
further of this video but there is very specific use case only in that case you can use lock
free mechanism and in that case you can achieve faster result than lock based.
But apart from this specific use case lock based mechanism is something which helps you
so that's why I wanted to clarify that it is not an alternative to lock based mechanism
But it is one of the way to achieve the concurrency for some specific use case. We will see that
Okay, so
Which operation or which technique is help?
to achieve lock-free mechanism that is cast operation compare and swap and
Java provide these four classes which uses this technique
right and helps to achieve the concurrency but before I tell you about
this cash operation before I tell you about the cash operation I want you to
have a just remind quickly about the optimistic locking right so you know
right optimistic locking in the previous video I have told you about the
optimistic locking or it is it or I would say that not optimistic locking I
would say that optimistic concurrency control. Optimistic concurrency control
because it's also a lock free. It also a lock free protocol and also what happen
in the optimistic is remember let's give one minute to quickly remind you because
this is very very important to link with CAS. Right because both are lock free
protocol right so let's say you have one row in a table so this is let's say
a roll number 1 2 3 student is Raj
was 1 when it reads. Now what it done is it made it modify something let's say
from Raj K it has put the change the name to Raj K and it has modified it to
Raj a Raja right so they have certain modification is done on the name right
and now they are trying to update the DB okay now let's say thread one coming
first so this is going first right so this is going for the update the DB so
it will try to let's say it will run the query update this table right you update
your table what it will do it will set
its name from Raj to Raj K and your version row version should be 1 what was
update this row right change Raj to Raj K right where roll number is 1 2 3 and
the row version is 1 so this will get success yes row version is 1 roll number