boolean (Semaphore semaphore, long timeoutInMillis) { long end = System.currentTimeMillis() + timeoutInMillis; long remaining = timeoutInMillis; do { try { return semaphore.tryAcquire(remaining, TimeUnit.MILLISECONDS); } catch (InterruptedException ignored) { remaining = end - System.currentTimeMillis(); } } while (remaining > 0); return false; }