annotate src/goodjava/lucene/logging/SemaphoreLock.java @ 1539:c27dc6af87ca

remove nextId
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 06 Sep 2020 11:35:30 -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 }