package ru.lionarius.sync; public class MySemaphore { private final Object lock = new Object(); private int permits; public MySemaphore(int permits) { this.permits = permits; } public void acquire() throws InterruptedException { synchronized (lock) { while (permits <= 0) { lock.wait(); } permits--; } } public boolean tryAcquire() { synchronized (lock) { if (permits <= 0) { return false; } permits--; return true; } } public void release() { synchronized (lock) { permits++; lock.notify(); } } public int availablePermits() { synchronized (lock) { return permits; } } }