#89 Race Condition in Java

preview_player
Показать описание
Check out our courses:

Coupon: TELUSKO10 (10% Discount)

Udemy Courses:

For More Queries WhatsApp or Call on : +919008963671

In this lecture we will learn:
- What are threads and mutations?
- Thread safe in Java
- Use of join() method in threads
- What is synchronization?
- Race condition in java

#1
Threads and Mutations:
- Threads are useful when you want to execute multiple things at the same time.
- Most of the time, threads are created by the framework itself.
- Threads are used when you want to make things faster.

- Mutations simply mean that you can change something.
- Primitive type variables and primitive type objects are mutations as their value can be changed.
- Strings are immutable as we cannot change their value of it.
- Use of threads and mutations at the same time is not good, as it creates instability in the code.

#2
Thread Safe:
Thread safe means that only one thread will work at one point.
- When a thread is already working on an object and preventing another thread from working on the same object, this process is called Thread-Safety.
- If we have two threads and each thread is calling increment thousand times, then increment will be called two thousand times.
- For the above case, every time you run the code you will get a different output for this.
- This happens because the main method prints the value of the count at any moment of time, it does not wait for threads to execute completely and come back to the main method.
- If the main method waits for threads to execute and to come back after completion, then it gives nearby correct output.

#3
join method and synchronized keyword:
- join() is a method that allows the main method to wait for the other threads to come back and join.
- join through an exception so we have to handle it by using throws Interruption.
- If both threads go to the method at the same time then it might be possible that they will be lost some of the values in between.
- The above problem will be resolved by using the synchronized keyword.
- By using the synchronized, java ensures that the method will be called by only one method at a time to handle instability in code.
- So, if a thread is working with the synchronized method, then the other thread has to wait to work with that method until the first thread gets completed.
- Synchronization in java is the capability to control the access of multiple threads to any shared resource.

#4
Race condition:
- Synchronization helps to prevent the race condition.
- Race condition is a condition in which the critical section (a part of the program where shared memory is accessed) is concurrently executed by two or more threads. It leads to incorrect behaviour of a program.

More Learning :

Donation:
PayPal Id : navinreddy20
Рекомендации по теме
Комментарии
Автор

Point to point explanation no time waste

xihadityajaiswal
Автор

Thank you for all of your videos. You are a fantastic teacher.

PRVLEED
Автор

Thank you very much for all your clear and understandable explanations,
You demonstrate and detail in a particularly understandable way!

ueecvlv
Автор

The best explanation for race condition. keep going

AhmedElsaadany-eyri
Автор

thanks for the clear explanation one more time

ShermukhammadKarimov
Автор

can volatile keyword used in this stuation instead of scynchronize? (int volatile count;)

nerdium
Автор

Thanks for your video, i took a video in udemy, i just lost money, after discovering your videos :D
I really love the simple way, you explained complex problem !

adhikaribinu
Автор

I am getting error as "Error: Could not find or load main class mythread", when i try executing the code

ragavir
Автор

Why when I try to execute by removing join of both the t1 and t2, with sync method it prints count zero and if try to print the count by joining only one t1 and not t2 it comes near to 20000 not complete 20000?

RishiRaj-xjzb
Автор

sir is there any logical mistake in my code :
class rider{
int count;// as it is an instance variable so it is by default zero
public synchronized void increment(){
count++;
}
}

class A extends rider implements Runnable{
public void run(){
for(int i=1;i<=10000;i++){
increment();
}
}
}
public class Hi{
public static void main(String[] args) throws InterruptedException{
rider c =new rider();
Runnable obj1=new A();
Runnable obj2=()->{
for(int i=1;i<=10000;i++){
c.increment();
}
};
Thread t1= new Thread(obj1);
Thread t2= new Thread(obj2);
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println(c.count);
}
}
is this process right or not

rockingstars
Автор

Hello Sir,
Very nice and beutiful explanation .
Just a small thing regarding the code, that in my System I am always getting correct value without using synchronize keyword . So I can't visualize the race condition . Do you have any explanation about it .
Thanks

kalashjain
Автор

Instead of synchronized we can use setpriority to any of the thread to avoid conflicts.. This block of code works for me ##

T1.start();
T1.sleep(5); // sleep condition
T2.start();

srikanthambati
Автор

class Counter{

int count;

public void increment(){
count++;
}
}

public class Main {
public static void main(String args[]) {

Counter c = new Counter();

Runnable call = () -> {
for (int i = 0; i < 100; i++) {
c.increment();
}

};

Runnable call2 = () -> {
for (int i = 0; i < 100; i++) {
c.increment();
}
};

Thread t1 = new Thread(call);
Thread t2 = new Thread(call2);


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



}
}

why am i getting (0)

NaveenKumar-hyet