If a timeout check on CountDownLatch.await is missing, the thread might unblock even when the latch has not counted down to zero. This might cause synchronization errors.
1public Object getResultNonCompliant(long timeout, TimeUnit unit)
2 throws InterruptedException, ExecutionException, TimeoutException {
3 // Noncompliant: code does not check if await timed out or the latch counted down to zero.
4 completionLatch.await(timeout, unit);
5 return result;
6}
1public Object getResultCompliant(long timeout, TimeUnit unit)
2 throws InterruptedException, ExecutionException, TimeoutException {
3 // Compliant: code handles the case when await times out.
4 if (!completionLatch.await(timeout, unit)) {
5 throw new TimeoutException();
6 }
7 return result;
8}