Code example for Lock

Methods: lock, tryLock, unlock

0
  public static void main(String args[]) throws InterruptedException, KeeperException {
    ZooQueueLock lock = new ZooQueueLock("/lock", true);
    DistributedReadWriteLock rlocker = new DistributedReadWriteLock(lock, "reader".getBytes());
    DistributedReadWriteLock wlocker = new DistributedReadWriteLock(lock, "wlocker".getBytes());
    Lock readLock = rlocker.readLock();
    readLock.lock();
    Lock readLock2 = rlocker.readLock();
    readLock2.lock();
    Lock writeLock = wlocker.writeLock();
    if (writeLock.tryLock(100, TimeUnit.MILLISECONDS))
      throw new RuntimeException("Write lock achieved during read lock!");
    readLock.unlock();
    readLock2.unlock();
    writeLock.lock();
    if (readLock.tryLock(100, TimeUnit.MILLISECONDS))
      throw new RuntimeException("Read lock achieved during write lock!");
    Lock writeLock2 = DistributedReadWriteLock.recoverLock(lock, "wlocker".getBytes());
    writeLock2.unlock();
    readLock.lock();
    System.out.println("success");
  }