Advertisement
Kostiggig

CustomReentrantLockSuccessExample

Apr 30th, 2023 (edited)
995
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.37 KB | None | 0 0
  1. package multithreading.lock.reentrant_custom_lock_success;
  2.  
  3. public class CustomReentrantLock {
  4.  
  5.     private boolean isLocked = false;
  6.     private Thread lockedBy = null;
  7.     private int lockedCount = 0;
  8.  
  9.     public void lock() throws InterruptedException {
  10.         synchronized(this) {
  11.             while(isLocked && Thread.currentThread() != lockedBy) wait();
  12.             lockedBy = Thread.currentThread();
  13.             lockedCount++;
  14.             isLocked = true;
  15.         }
  16.     }
  17.  
  18.     public void unlock() {
  19.         synchronized(this) {
  20.             if(Thread.currentThread() == lockedBy) {
  21.                 lockedCount--;
  22.                 if(lockedCount == 0) {
  23.                     lockedBy = null;
  24.                     isLocked = false;
  25.                     notify();
  26.                 }
  27.             }
  28.         }
  29.     }
  30.  
  31. }
  32.  
  33.  
  34. package multithreading.lock.reentrant_custom_lock_success;
  35.  
  36. import multithreading.lock.custom_lock.CustomLock;
  37.  
  38. public class ReentrantCustomLockSuccessExample {
  39.  
  40.     private final CustomReentrantLock lock = new CustomReentrantLock();
  41.     public synchronized void outer() throws InterruptedException {
  42.         lock.lock();
  43.         System.out.println(Thread.currentThread().getName() + " enters OUTER");
  44.         Thread.sleep(500);
  45.         inner();
  46.         lock.unlock();
  47.     }
  48.  
  49.     private synchronized void inner() throws InterruptedException {
  50.         lock.lock();
  51.         System.out.println(Thread.currentThread().getName() + " tries to enter INNER");
  52.         System.out.println(Thread.currentThread().getName() + " enters INNER");
  53.         lock.unlock();
  54.     }
  55.  
  56. }
  57.  
  58.  
  59. package multithreading.lock.reentrant_custom_lock_success;
  60.  
  61. public class ReentrantCustomLockSuccessExampleClient {
  62.  
  63.     public static void main(String[] args) throws InterruptedException {
  64.         ReentrantCustomLockSuccessExample reentrantCustomLockFailExample = new ReentrantCustomLockSuccessExample();
  65.         for (int i = 0; i < 5; i++) {
  66.             new Thread(() -> {
  67.                 try {
  68.                     reentrantCustomLockFailExample.outer();
  69.                 } catch (InterruptedException e) {
  70.                     throw new RuntimeException(e);
  71.                 }
  72.             }, "Thread " + i).start();
  73.         }
  74.  
  75.         Thread.sleep(7000); // needed to wait all threads to be completed (I'm lazy to call join on them after starting =) )
  76.     }
  77. }
  78.  
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement