Lock Implementations in Java |

Поделиться
HTML-код
  • Опубликовано: 8 ноя 2024

Комментарии • 3

  • @IndianPoliticsMemeShow
    @IndianPoliticsMemeShow 11 месяцев назад +1

    👍

  • @Werewolf5684
    @Werewolf5684 5 месяцев назад +1

    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"

    • @LazzyProgrammer
      @LazzyProgrammer  4 месяца назад

      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();
      }
      }
      }