#89 Race Condition in Java

  Рет қаралды 64,017

Telusko

Telusko

Күн бұрын

Check out our courses:
Enterprise Java Spring Microservices: go.telusko.com...
Coupon: TELUSKO10 (10% Discount)
Master Java Spring Development : go.telusko.com...
Coupon: TELUSKO20 (20% Discount)
For More Queries WhatsApp or Call on : +919008963671
Udemy Courses:
Spring: go.telusko.com...
Java:- go.telusko.com...
Java Spring:- go.telusko.com...
Java For Programmers:- go.telusko.com...
Python : go.telusko.com...
Git : go.telusko.com...
Docker : go.telusko.com...
website : courses.telusk...
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.
Github repo : github.com/nav...
Java:- bit.ly/JavaUde...
Spring:- bit.ly/SpringU...
More Learning :
Java :- bit.ly/3x6rr0N
Python :- bit.ly/3GRc7JX
Django :- bit.ly/3MmoJK6
JavaScript :- bit.ly/3tiAlHo
Node JS :- bit.ly/3GT4liq
Rest Api :-bit.ly/3MjhZwt
Servlet :- bit.ly/3Q7eA7k
Spring Framework :- bit.ly/3xi7buh
Design Patterns in Java :- bit.ly/3MocXiq
Docker :- bit.ly/3xjWzLA
Blockchain Tutorial :- bit.ly/3NSbOkc
Corda Tutorial:- bit.ly/3thbUKa
Hyperledger Fabric :- bit.ly/38RZCRB
NoSQL Tutorial :- bit.ly/3aJpRuc
Mysql Tutorial :- bit.ly/3thpr4L
Data Structures using Java :- bit.ly/3MuJa7S
Git Tutorial :- bit.ly/3NXyCPu
Donation:
PayPal Id : navinreddy20
www.telusko.com

Пікірлер: 38
#90 Thread States in Java
3:31
Telusko
Рет қаралды 47 М.
#88 Runnable vs Thread in Java
8:27
Telusko
Рет қаралды 75 М.
Underwater Challenge 😱
00:37
Topper Guild
Рет қаралды 48 МЛН
Incredible Dog Rescues Kittens from Bus - Inspiring Story #shorts
00:18
Fabiosa Best Lifehacks
Рет қаралды 37 МЛН
АЗАРТНИК 4 |СЕЗОН 1 Серия
40:47
Inter Production
Рет қаралды 1,3 МЛН
Фейковый воришка 😂
00:51
КАРЕНА МАКАРЕНА
Рет қаралды 6 МЛН
Java Strings are Immutable - Here's What That Actually Means
7:06
Coding with John
Рет қаралды 613 М.
#95 Comparator vs Comparable in Java
15:43
Telusko
Рет қаралды 176 М.
Learn Any Programming Language In 3 Hours!
22:37
Code With Huw
Рет қаралды 363 М.
#86 Multiple Threads in Java
11:30
Telusko
Рет қаралды 119 М.
#92 ArrayList in Java
11:54
Telusko
Рет қаралды 106 М.
Multithreading in Java Explained in 10 Minutes
10:01
Coding with John
Рет қаралды 917 М.
Functions vs Classes: When to Use Which and Why?
10:49
ArjanCodes
Рет қаралды 154 М.
Deadlock in Java
17:48
Jakob Jenkov
Рет қаралды 22 М.
Underwater Challenge 😱
00:37
Topper Guild
Рет қаралды 48 МЛН