# java.util.concurrent.locks

***

**1. Thread Synchronization with ReentrantLock**

```java
public class ReentrantLockExample {
    private final ReentrantLock lock = new ReentrantLock();
    private int count = 0;

    public void increment() {
        try {
            lock.lock();
            count++;
        } finally {
            lock.unlock();
        }
    }

    public int getCount() {
        try {
            lock.lock();
            return count;
        } finally {
            lock.unlock();
        }
    }
}
```

**2. Inter-Thread Communication with Condition Variables**

```java
public class ConditionVariableExample {
    private final Object lock = new Object();
    private boolean dataReady = false;

    private void produceData() {
        synchronized (lock) {
            dataReady = true;
            lock.notifyAll();
        }
    }

    private void consumeData() {
        synchronized (lock) {
            while (!dataReady) {
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            // Consume data
        }
    }
}
```

**3. Reader-Writer Lock for Concurrent Data Access**

```java
public class ReaderWriterLockExample {
    private final ReadWriteLock lock = new ReentrantReadWriteLock();

    private int data = 0;

    public void read() {
        try {
            lock.readLock().lock();
            System.out.println(data);
        } finally {
            lock.readLock().unlock();
        }
    }

    public void write() {
        try {
            lock.writeLock().lock();
            data++;
        } finally {
            lock.writeLock().unlock();
        }
    }
}
```

**4. Semaphore for Resource Allocation**

```java
public class SemaphoreExample {
    private final Semaphore semaphore = new Semaphore(3);

    public void accessResource() {
        try {
            semaphore.acquire();
            // Use resource
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            semaphore.release();
        }
    }
}
```

**5. CountDownLatch for Synchronizing Events**

```java
public class CountDownLatchExample {
    private final CountDownLatch latch = new CountDownLatch(3);

    public void task1() {
        try {
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // Execute task 1
    }

    public void task2() {
        // Execute task 2
        latch.countDown();
    }

    public void task3() {
        // Execute task 3
        latch.countDown();
    }
}
```

**6. Phaser for Parallel Tasks**

```java
public class PhaserExample {
    private final Phaser phaser = new Phaser(3);

    public void task1() {
        // Execute task 1
        phaser.arriveAndAwaitAdvance();
    }

    public void task2() {
        // Execute task 2
        phaser.arriveAndAwaitAdvance();
    }

    public void task3() {
        // Execute task 3
        phaser.arriveAndDeregister();
    }
}
```

**7. Exchanger for Data Exchange**

```java
public class ExchangerExample {
    private final Exchanger<Object> exchanger = new Exchanger<>();

    public void swapData() {
        try {
            Object data1 = exchanger.exchange(new Object());
            Object data2 = exchanger.exchange(data1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
```

**8. StampedLock for Optimistic Concurrency**

```java
public class StampedLockExample {
    private final StampedLock lock = new StampedLock();

    private int data = 0;

    public void read() {
        long stamp = lock.tryOptimisticRead();
        int currentData = data;
        if (!lock.validate(stamp)) {
            stamp = lock.readLock();
            try {
                currentData = data;
            } finally {
                lock.unlockRead(stamp);
            }
        }
        // Use data
    }

    public void write() {
        long stamp = lock.writeLock();
        try {
            data++;
        } finally {
            lock.unlockWrite(stamp);
        }
    }
}
```

**9. Synchronizer for Thread Synchronization**

```java
public class SynchronizerExample {
    private final Synchronizer<Object> synchronizer = new Synchronizer<>();

    public void task1() {
        try {
            Object result = synchronizer.acquireInterruptibly(1000);
            // Process result
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void task2() {
        // Generate result
        synchronizer.release(result);
    }
}
```

**10. LockSupport for Thread Suspension**

```java
public class LockSupportExample {
    public void suspendThread() {
        Thread thread = new Thread(() -> {
            while (true) {
                LockSupport.park(); // Suspend thread
            }
        });
        thread.start();
    }

    public void resumeThread() {
        LockSupport.unpark(thread); // Resume thread
    }
}
```

**11. Semaphore with Fairness**

```java
public class FairSemaphoreExample {
    private final Semaphore semaphore = new Semaphore(3, true);

    public void accessResource() {
        try {
            semaphore.acquire();
            // Use resource
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            semaphore.release();
        }
    }
}
```

**12. ReentrantLock with Timeout**

```java
public class ReentrantLockWithTimeoutExample {
    private final ReentrantLock lock = new ReentrantLock();

    public void lockWithTimeout() {
        try {
            if (lock.tryLock(1000, TimeUnit.MILLISECONDS)) {
                // Acquire lock
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
```

**13. CyclicBarrier for Synchronizing Threads**

```java
public class CyclicBarrierExample {
    private final CyclicBarrier barrier = new CyclicBarrier(3);

    public void task1() {
        try {
            barrier.await();
        } catch (InterruptedException | BrokenBarrierException e) {
            e.printStackTrace();
        }
        // Execute task 1
    }

    public void task2() {
        try {
            barrier.await();
        } catch (InterruptedException | BrokenBarrierException e) {
            e.printStackTrace();
        }
        // Execute task 2
    }

    public void task3() {
        try {
            barrier.await();
        } catch (InterruptedException | BrokenBarrierException e) {
            e.printStackTrace();
        }
        // Execute task 3
    }
}
```

**14. StampedLock with Read-Write Locks**

```java
public class StampedLockWithReadWriteLocksExample {
    private final StampedLock lock = new StampedLock();

    private int data = 0;

    public void read() {
        long stamp = lock.readLock();
        try {
            int currentData = data;
        } finally {
            lock.unlockRead(stamp);
        }
    }

    public void write() {
        long stamp = lock.writeLock();
        try {
            data++;
        } finally {
            lock.unlockWrite(stamp);
        }
    }
}
```

**15. CountDownLatch with Multiple Awaiters**

```java
public class CountDownLatchWithMultipleAwaitersExample {
    private final CountDownLatch latch = new CountDownLatch(3);

    public void task1() {
        try {
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // Execute task 1
    }

    public void task2() {
        try {
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // Execute task 2
    }

    public void task3() {
        latch.countDown();
    }

    public void task4() {
        latch.countDown();
    }

    public void task5() {
        latch.countDown();
    }
}
```

**16. Exchanger with Object Transfer**

```java
public class ExchangerWithObjectTransferExample {
    private final Exchanger<Object> exchanger = new Exchanger<>();

    public void task1() {
        try {
            Object object = exchanger.exchange(new Object());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void task2() {
        try {
            Object object = exchanger.exchange(new Object());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
```

**17. ConditionVariable with Multiple Waiters**

```java
public class ConditionVariableWithMultipleWaitersExample {
    private final Object lock = new Object();
    private final ConditionVariable cv = lock.newCondition();
    private boolean dataReady = false;

    public void producer() {
        synchronized (lock) {
            while (!dataReady) {
                try {
                    cv.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            // Produce data
        }
    }

    public void consumer() {
        synchronized (lock) {
            dataReady = true;
            cv.signalAll();
        }
    }
}
```

**18. StampedLock with Write-Write Lock**

```java
public class StampedLockWithWriteWriteLockExample {
    private final StampedLock lock = new StampedLock();

    private int data = 0;

    public void write1() {
        long stamp = lock.writeLock();
        try {
            data++;
        } finally {
            lock.unlockWrite(stamp);
        }
    }

    public void write2() {
        long stamp = lock.tryWriteLock(1000, TimeUnit.MILLISECONDS);
        try {
            data++;
        } finally {
            lock.unlockWrite(stamp);
        }
    }
}
```

**19. ConditionVariable with Timed Waiting**

```java
public class ConditionVariableWithTimedWaitingExample {
    private final Object lock = new Object();
    private final ConditionVariable cv = lock.newCondition();
    private boolean dataReady = false;

    public void producer() {
        synchronized (lock) {
            dataReady = true;
            cv.signalAll();
        }
    }

    public void consumer() {
        synchronized (lock) {
            try {
                cv.await(1000, TimeUnit.MILLISECONDS);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (dataReady) {
                // Consume data
            }
        }
    }
}
```

**20. ReaderWriterLock with Priority**

```java
public class ReaderWriterLockWithPriorityExample {
    private final ReadWriteLock lock = new ReentrantReadWriteLock(true);

    private int data = 0;

    public void read() {
        try {
            lock.readLock().lock();
            int currentData = data;
        } finally {
            lock.readLock().unlock();
        }
    }

    public void write() {
        try {
            lock.writeLock().lock();
            data++;
        } finally {
            lock.writeLock().unlock();
        }
    }
}
```

**21. ReentrantLock with Interrupt Handling**

```java
public class ReentrantLockWithInterruptHandlingExample {
    private final ReentrantLock lock = new ReentrantLock();

    public void lockWithInterruptHandling() {
        try {
            lock.lockInterruptibly();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
```

**22. CyclicBarrier with BarrierAction**
