Java: Using Semaphore with timer



Hello im trying to synchronize classes using semaphore.acquire() and semaphore.release(). I’m calling semaphore.release() from a class named ReadSerialPort and semaphore.acquire() from WriteSerialPort to connect a phone-center to PC. My problem is that phone-center is not always responding.

I would like to know a way to set timer and make semaphore.release() after the time has past.

P.S: I tried using TimedSemaphore classes but nothing happened

Answer

The answer of my question was solved by Jan Goynvaeis using semaphore.tryAcquire(10,TimeUnit.SECONDS) so the acquire will time out after 10 seconds . After the timeout im using semaphore.release(0) to clear the thread



Source: stackoverflow