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

preview_player
Показать описание
➡️ 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:

Chapters:
00:00 - Understanding CAS (Compare and Swap)
15:06 - Atomic Variables
32:48 - Volatile Variables
38:53 - Concurrent Collection

#java #softwareengineer #multithreading
Рекомендации по теме
Комментарии
Автор

Thank u so much Shrayansh you are a legend

AbdelrhmanAdel-yb
Автор

Hi Shrayansh, isn't it true that Nonvolatile variables are also stored in the main memory. Still, JVM might use a caching mechanism for performance which affects the visibility of the variable in a concurrent environment. For volatile it guarantees that the value is never cached and always read from the main memory.

amar_
Автор

Hi Shrayansh, Can you provide some hands on questions where we can practice this multithreading concepts and implement it

LaptopBackup-bokp
Автор

To test this as I needed to wait for both the threads to get completed then i can print the value of the counter. I have used "CountDownLatch". Here we can specify the no of threads for which we can wait before displaying or executing other task.

public class ThreadCounterProblem {

int counter=0;

CountDownLatch latch = new CountDownLatch(2);

void testThreadCounterProblem() {

Thread t1 = new Thread(()-> {
for(int i=0;i<100;i++) {
increment();
}
latch.countDown();
});

Thread t2 = new Thread(()-> {
for(int i=0;i<100;i++) {
increment();
}
latch.countDown();
});

t1.start();
t2.start();

try {
latch.await(); // Wait for both threads to signal completion
} catch (InterruptedException e) {
e.printStackTrace();
}

displayFinalCounterValue();
}

void increment() {
counter++;
}

void displayFinalCounterValue() {
System.out.println("Counter value : "+this.counter);
}
}

zaheerabass
Автор

CAS alone doesn't guarantee thread safety, it just guarantees Atomicity . CAS + Volatile guarantees thread safety + Atomicity .

yt_se_yt
Автор

it must be internally taking lock right otherwise if two request come for CAS at the same time then for both value stored will be same

nishantkumar
Автор

hi, about java series/playlist how many more videos/topics we can expect roughly ?

easward
Автор

so the main core thing about Atomic Variables is that CAS operation is Atomic, but I don't get this ki why and how CAS is atomic. Do i need to crame the CAS is atomic or they are internally using some multithreading conepts ?

indianengineer
Автор

one doubt related to optimistic concurrency control it is lock free ok
both thread read the value at the same time with rowVersion=1, one thread updates the name and rowVersion=2 when second thread will do it he will get an error and will read the name again and update the rowVersion to 3
doubt is when both tries to give update instruction at the same time to DB when rowVersion=1 then what should be behaviour
DB will give chance to lets thread 2 first it will update the name and rowVersion to 2 and thread 1 is waiting
and after updating now thread 1 get the chance of updating but then their is a mismatching with the where condition so it will do all the things again
am I thinking here right?
if yes so like db is giving chance to 1 of the threads means that thread has acquired the lock on that row and other is waiting for that lock on that row and then it is not a lock free concurrency control? I got the idea of this concurrency control that read requires no lock but write parrallely for one row can happen then what will happen on that case?

Aakashkumar-regk
Автор

I have enrolled u r course where I can get Notes?

dhoniramakrishna
Автор

@ConceptandCoding, I'm interested in unlocking access to Java, LLD, and HLD videos. Could I receive for all upcoming content in these categories without any cost .

vinayprakashyadav
Автор

when are you going to start Spring Boot ?

shahidkhan
Автор

Hey, can you also cover java Concurrency package in multithreading.

ritikpandey
Автор

at 10:35 let say Inside CAS fn step2 is successfull that is old val == load val from mem
and it enters in if codition to update value then at that point context switch happens(before updating) and thread2 comes did all three step updated the value and then thread1 resumes and just update the value

isn't it creating inconsistency??? can anyone help here??

piyushpathak
Автор

I run the counter example with multiple threads without using atomicInteger and get the expected result. As shown in video the output is 371 when multiple thread increase the (int counter ++) counter, but when i run that example I got 400 as output. I'm little confused here, why I'm seeing such behaviour ?

ritikpandey
Автор

Hey, how to access member only video? Please help me.

alauddintuhin