Тёмный

Lock Implementations in Java |  

Lazy Programmer
Подписаться 4,5 тыс.
Просмотров 663
50% 1

Today we are going to explore Locks in Java.
Locks act like a token required to access a shared resource. If a thread has the token , it can access the resource. If not, it must wait until the token is available.
In this video we have explained what is a Reentrant, ReadWrite and Stamped Locks. With Real World Examples and Code Snippets.
Don't miss this opportunity to level up your Java programming skills! Hit that like button, subscribe for more Java tutorials, and let's get started on this multithreading adventure.
You can access the specific topic from the chapters given below
0:00 Introduction
0:23 What is #ReentrantLock?
0:48 Real World Example of ReentrantLock
1:11 #Code Implementation
2:00 Code #Walkthrough
2:54 Why #finally block to #release the #Locks?
3:34 What is #ReadWriteLock?
4:11 What is Exclusive Access?
4:40 Code Implementation
2:00 Code Walkthrough
5:23 Another Real Example of Locks
6:34 Use case for ReadWriteLock
7:29 What is StampedLock?
7:51 Real World Example
8:21 Purpose of Optimistic Read Locks
8:35 Code Implementation/Walkthrough
10:34 Use case for StampedLock
11:10 ReadWrite VS Stamped Lock
Programmer T-Shirt: teeshopper.in/products/Progra...
India-Sachin Hoodie: teeshopper.in/products/India-...
Virat Kohli T-Shirt: teeshopper.in/products/Virat-...
Checkout the cool T-Shirt for Bharat with Flag: teeshopper.in/products/Bharat...
To check out more on the tutorials Topic wise you can follow below links
Links:
Please do checkout other tutorial videos also if required:
Java Multithreading: • How to Create and Run ...
Spring Framework: • Spring Framework
Java 21: • Java 21 Features | All...
DevOps: • DevOps
Java Design Patterns: • Design Patterns
Java 8 Features: • Java 8
Core Java Complete Tutorial: • Core Java Complete Guide
Interview Preparation for Java: • Interview Preparation ...
Python: • Python
Linux: • Linux
Please do LIKE, Share and SUBSCRIBE
Java multithreading, multithreading in Java, Java threads, multithreading concepts, Java programming, thread synchronization, thread safety, multithreaded applications, Java performance, programming tutorial, core Java, Java development, coding examples, Java concurrency, Java tutorials, learn Java multithreading, Java coding, optimize Java code.
#JavaProgramming #MultithreadingInJava #JavaThreads #ProgrammingTutorial #JavaPerformance #CodeOptimization #virtualthreads #java #new-features #java21 #exceptionhandlinginjava #interview #generics #hashtable #interview #jdbc #java #db #treeset #collectionframework #binarysearchtree #linkedlist #list #arraylist #fibonacci #java #fastest #gc #heap #java #stack #jvm #architecture #exception #interview #exam #interface #abstraction #coding #methodoverloading #methodoverriding #runtime #compiletime #polymorphism #inheritance #constructors #objects #static #eclipse #developer #installation #download #awesome #youtube #youtuber #youtubers #subscribe #youtubevideos #sub #youtubevideo #like #identifiers #operators #variables #typesofvariables #innerclasses #static

Опубликовано:

 

28 июн 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 3   
@IndianPoliticsMemeShow
@IndianPoliticsMemeShow 6 месяцев назад
👍
@Werewolf5684
@Werewolf5684 25 дней назад
Hey I am facing some problem while implementing readWriteLock. Can you please let me know what is the problem I am making here So there is a class which having some value [so it is a critical section], Now from our main class I am creating 5 read thread to read the data from that critical section 5 times and 5 write thread to update the data. so the expectation is I want to have same value for a particular thread for every iteration, and while updating the value another thread should read the value import java.util.concurrent.locks.ReentrantReadWriteLock; public class Node extends ReentrantReadWriteLock { private T value; public Node(T value) { this.value = value; } public void setValue(T value) { this.value = value; } public T getValue() { return this.value; } } import java.util.concurrent.ThreadLocalRandom; public class ReadWriteLockDemo2 { private static Node resource = new Node(5); public static void main(String[] args) { for (int i = 0 ; i < 5 ; i++) { new Thread(() -> { resource.readLock(); try { for (int j = 0 ; j < 5 ; j++) { System.out.println(Thread.currentThread().getName() + " is reading the data " + resource.getValue()); try { Thread.sleep(ThreadLocalRandom.current().nextInt(1000, 5000)); } catch (InterruptedException e) { throw new RuntimeException(e); } } } finally { resource.readLock().unlock(); } }).start(); } for (int i = 0 ; i < 5 ; i++) { new Thread(() -> { resource.writeLock(); try { int value = ThreadLocalRandom.current().nextInt(1, 100); System.out.println(Thread.currentThread().getName() + " is writing the data with " + value); resource.setValue(value); try { Thread.sleep(ThreadLocalRandom.current().nextInt(1000, 5000)); } catch (InterruptedException e) { throw new RuntimeException(e); } } finally { resource.writeLock().unlock(); } }).start(); } } } I feel like locks are not working properly can you check I am getting first time iteration all read are working then all write are working and updating quickly [updating the value at the same time] where as they have to wait because that's a write lock but they are not waiting. Also I am getting this 2 error not sure where I am making this mistake, "attempt to unlock read lock, not locked by current thread"
@TheRealLazyProgrammer
@TheRealLazyProgrammer 7 дней назад
Your current code can lead to a deadlock scenario, If a read thread holds the read lock and a write thread tries to acquire the write lock, they’ll block each other indefinitely. Additionally, You have extended ReentrantReadWriteLock in your Node class. This is not the correct way to use it. Instead, create an instance of ReentrantReadWriteLock within your Node class and use it for synchronization. Can you try below code: import java.util.concurrent.locks.ReentrantReadWriteLock; class Node { private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); private T value; public Node(T value) { this.value = value; } public void setValue(T value) { lock.writeLock().lock(); try { this.value = value; } finally { lock.writeLock().unlock(); } } public T getValue() { lock.readLock().lock(); try { return value; } finally { lock.readLock().unlock(); } } } public class ReadWriteLockDemo { public static void main(String[] args) { Node resource = new Node(5); // Create read threads for (int i = 0; i < 5; i++) { new Thread(() -> { for (int j = 0; j < 5; j++) { System.out.println(Thread.currentThread().getName() + " is reading the data: " + resource.getValue()); try { Thread.sleep(1000); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } }).start(); } // Create write threads for (int i = 0; i < 5; i++) { new Thread(() -> { int value = (int) (Math.random() * 100); System.out.println(Thread.currentThread().getName() + " is writing the data with value: " + value); resource.setValue(value); try { Thread.sleep(1000); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } }).start(); } } }
Далее
Java Lock
28:51
Просмотров 43 тыс.
Gale Now VS Then Edit🥵 #brawlstars #shorts
00:15
Просмотров 376 тыс.
Recycled Car Tyres Get a Second Life! ♻️
00:58
Просмотров 3,5 млн
Optimistic Locking - What, When, Why, and How?
16:34
Просмотров 12 тыс.
Java ReentrantLock - fairness, tryLock and more
13:17
Просмотров 130 тыс.
Multithreading in Java Explained in 10 Minutes
10:01
Просмотров 884 тыс.
ThreadLocal in Java
10:59
Просмотров 180 тыс.