annotate src/goodjava/lucene/logging/SemaphoreLock.java @ 1554:83249ce59d13

minor fix
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 06 Oct 2020 19:36:57 -0600
parents 634f6765830e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1538
634f6765830e use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 package goodjava.lucene.logging;
634f6765830e use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2
634f6765830e use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 import java.util.concurrent.Semaphore;
634f6765830e use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 import java.util.concurrent.TimeUnit;
634f6765830e use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5
634f6765830e use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6
634f6765830e use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 public final class SemaphoreLock {
634f6765830e use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 private final Semaphore semaphore = new Semaphore(1);
634f6765830e use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9
634f6765830e use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 public void unlock() {
634f6765830e use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 semaphore.release();
634f6765830e use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 }
634f6765830e use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13
634f6765830e use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 public boolean isLocked() {
634f6765830e use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 return semaphore.availablePermits() == 0;
634f6765830e use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 }
634f6765830e use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17
634f6765830e use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 public boolean tryLock(long time,TimeUnit unit) throws InterruptedException {
634f6765830e use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 return semaphore.tryAcquire(time,unit);
634f6765830e use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 }
634f6765830e use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 }