Advertisement
Jaydeep999997

ReadWrite Lock

Oct 22nd, 2024
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.00 KB | Source Code | 0 0
  1. class DemonstrationOfReadWriteLock {
  2.   public static void main(String[] args) throws InterruptedException {
  3.     ReadWriteLock lock = new ReadWriteLock();
  4.  
  5.     Thread writer1 =
  6.         new Thread(
  7.             () -> {
  8.               try {
  9.                 System.out.println("Writer 1 Attempting to acquire write lock");
  10.                 lock.acquireWriteLock();
  11.                 System.out.println("Writer 1 Acquired write lock");
  12.               } catch (InterruptedException e) {
  13.  
  14.               }
  15.             });
  16.  
  17.     Thread writer2 =
  18.         new Thread(
  19.             () -> {
  20.               try {
  21.                 System.out.println("Writer 2 Attempting to acquire write lock");
  22.                 lock.acquireWriteLock();
  23.                 System.out.println("Writer 2 Acquired write lock");
  24.               } catch (InterruptedException e) {
  25.  
  26.               }
  27.             });
  28.  
  29.     Thread writer3 =
  30.         new Thread(
  31.             () -> {
  32.               try {
  33.                 System.out.println("Writer 3 Attempting to release write lock");
  34.                 lock.releaseWriteLock();
  35.                 System.out.println("Writer 3 Released write lock");
  36.               } catch (InterruptedException e) {
  37.  
  38.               }
  39.             });
  40.  
  41.     Thread reader1 =
  42.         new Thread(
  43.             new Runnable() {
  44.               @Override
  45.               public void run() {
  46.                 try {
  47.                   System.out.println("Reader 1 Attempting to acquire read lock");
  48.                   lock.acquireReadLock();
  49.                   System.out.println("Reader 1 Acquired read lock");
  50.                 } catch (InterruptedException e) {
  51.  
  52.                 }
  53.               }
  54.             });
  55.  
  56.     Thread reader2 =
  57.         new Thread(
  58.             () -> {
  59.               try {
  60.                 System.out.println("Reader 2 Attempting to release read lock");
  61.                 lock.releaseReadLock();
  62.                 System.out.println("Reader 2 Released read lock");
  63.               } catch (InterruptedException e) {
  64.  
  65.               }
  66.             });
  67.  
  68.     reader1.start();
  69.     Thread.sleep(100);
  70.  
  71.     writer1.start();
  72.     Thread.sleep(100);
  73.  
  74.     reader2.start();
  75.  
  76.     reader1.join();
  77.     reader2.join();
  78.     Thread.sleep(100);
  79.  
  80.     writer2.start();
  81.     Thread.sleep(100);
  82.  
  83.     writer3.start();
  84.     Thread.sleep(100);
  85.  
  86.     writer1.join();
  87.     writer2.join();
  88.     writer3.join();
  89.   }
  90. }
  91.  
  92. class ReadWriteLock {
  93.   private int readers = 0;
  94.   private boolean writer = false;
  95.  
  96.   public synchronized void acquireReadLock() throws InterruptedException {
  97.     while (writer) {
  98.       wait();
  99.     }
  100.     readers++;
  101.   }
  102.  
  103.   public synchronized void releaseReadLock() throws InterruptedException {
  104.     readers--;
  105.     notify();
  106.   }
  107.  
  108.   public synchronized void acquireWriteLock() throws InterruptedException {
  109.     while (writer || readers > 0) {
  110.       wait();
  111.     }
  112.     writer = true;
  113.   }
  114.  
  115.   public synchronized void releaseWriteLock() throws InterruptedException {
  116.     writer = false;
  117.     notifyAll();
  118.   }
  119. }
  120.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement