1. Tubelator AI
  2. >
  3. Videos
  4. >
  5. Education
  6. >
  7. 33. Lock-Free Concurrency | Compare-and-Swap | Atomic & Volatile Variables | Multithreading Part5

33. Lock-Free Concurrency | Compare-and-Swap | Atomic & Volatile Variables | Multithreading Part5

Available In Following Subtitles
English
Variant 1
Posted on:
➡️ 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
tubelator logo

Instantly generate YouTube summary, transcript and subtitles!

chrome-icon Install Tubelator On Chrome

Video Summary & Chapters

No chapters for this video generated yet.

Video Transcript

0:01
Hey guys, welcome to concept encoding and this is Shrayansh and today in multi threading
0:06
We are going to cover this two topics atomic variables
0:10
And also we will see the difference with the volatile and also the concurrent collections
0:15
We have already discussed concurrent collection during the collections topics when we were discussing
0:20
Right, but we will have a quick look at it
0:24
So let's start without wasting any time
0:28
So, here if an interview, if anybody asked you, hey, in how many ways you can achieve
0:35
the concurrency?
0:38
What would your answer would be?
0:40
So there are two ways in which we can achieve the concurrency one is the lock based mechanism
0:47
Another is lock free mechanism
0:51
So lock based we have already seen so many different ways synchronized
0:55
Rent-rent, stamp, read-write, semaphores all this we have seen already
1:01
But there is another way lock free mechanism
1:06
Right in this we have put locks
1:38
right to make sure that only one thread
1:39
can be used at very different scenarios where we have lot of complexity, where we
1:46
have lot of business logics. This will comes into the picture. Lock-free
1:52
mechanism is used very specific area, very very specific use case I would say
1:58
that. Okay I will tell you about that use case slowly when we will go into the
2:04
further of this video but there is very specific use case only in that case you can use lock
2:10
free mechanism and in that case you can achieve faster result than lock based.
2:17
But apart from this specific use case lock based mechanism is something which helps you
2:23
so that's why I wanted to clarify that it is not an alternative to lock based mechanism
2:28
But it is one of the way to achieve the concurrency for some specific use case. We will see that
2:36
Okay, so
2:39
Which operation or which technique is help?
2:44
to achieve lock-free mechanism that is cast operation compare and swap and
2:51
Java provide these four classes which uses this technique
2:56
right and helps to achieve the concurrency but before I tell you about
3:03
this cash operation before I tell you about the cash operation I want you to
3:08
have a just remind quickly about the optimistic locking right so you know
3:13
right optimistic locking in the previous video I have told you about the
3:18
optimistic locking or it is it or I would say that not optimistic locking I
3:23
would say that optimistic concurrency control. Optimistic concurrency control
3:28
because it's also a lock free. It also a lock free protocol and also what happen
3:36
in the optimistic is remember let's give one minute to quickly remind you because
3:41
this is very very important to link with CAS. Right because both are lock free
3:47
protocol right so let's say you have one row in a table so this is let's say
3:53
a roll number 1 2 3 student is Raj
4:24
was 1 when it reads. Now what it done is it made it modify something let's say
4:36
from Raj K it has put the change the name to Raj K and it has modified it to
4:46
Raj a Raja right so they have certain modification is done on the name right
4:54
and now they are trying to update the DB okay now let's say thread one coming
5:01
first so this is going first right so this is going for the update the DB so
5:06
it will try to let's say it will run the query update this table right you update
5:15
your table what it will do it will set
5:18
its name from Raj to Raj K and your version row version should be 1 what was
5:45
update this row right change Raj to Raj K right where roll number is 1 2 3 and
5:54
the row version is 1 so this will get success yes row version is 1 roll number
shape-icon

Download extension to view full transcript.

chrome-icon Install Tubelator On Chrome