Reentant locks


public class ReentrLockEx {

  final Lock lock = new ReentrantLock();

  void keepLock(int limit) {
    String t = currentThread().getName();
    lock.lock();
    out.println(t + ",acquired and keeping lock+ ");
    try {
      for (int i = limit; i > 0; i--) {
        out.println(t+",keep lock, left"+i+","+sec());
        sleepSeconds(1);
      }
    } finally {
      lock.unlock();
    }

  }

  void tryToAcquireLock() {
    String thread = currentThread().getName();
    out.println(thread + ",trying to aquire lock ");
    boolean result = lock.tryLock();
    try {
      if (result)
        out.println(thread + ",aquired lock ");
      else
        out.println(thread + ",failed to aquire");
    } finally {
      if (result)
        lock.unlock();
    }

  }

  void acquireLock() {
    String thread = currentThread().getName();
    out.println(thread + " aquire lock ");
    lock.lock();
    try {
      System.out.println(thread + " aquired ");
    } finally {
      lock.unlock();
    }
  }

  public static void main(String[] args) {
    ReentrLockEx rlEx = new ReentrLockEx();
    rlEx.createKeeping(rlEx, 5).start();
    sleepSeconds(1);
    rlEx.createTryToAcquire(rlEx).start();
    sleepSeconds(1);
    rlEx.createAquiring(rlEx).start();
  }


  Thread createAquiring(final ReentrLockEx rlEx) {
    return new Thread(new Runnable() {

      @Override
      public void run() {
        rlEx.acquireLock();
      }
    });
  }

  Thread createKeeping(final ReentrLockEx rlEx, final int limit) {
    return new Thread(new Runnable() {

      @Override
      public void run() {
        rlEx.keepLock(limit);
      }
    });
  }

  Thread createTryToAcquire(final ReentrLockEx rlEx) {
    return new Thread(new Runnable() {

      @Override
      public void run() {
        rlEx.tryToAcquireLock();
      }
    });
  }


  static void sleepSeconds(int n) {
    try {
      Thread.sleep(1000 * n);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }

  static String sec() {
    long currentTimeMillis = currentTimeMillis();
    long value = currentTimeMillis / 1000;
    return ", second " + (value - (value / 100 * 100));
  }

}

Result:

// thread0 acquired the lock and will keep it for 5 secs

Thread-0,acquired and keeping lock
Thread-0,keep lock, left 5 , second 0
Thread-0,keep lock, left 4 , second 1

// thread1 will try to acquire the lock, but fails (not blocking, returns false)
// to obtain it since the lock is still used by thread0

Thread-1,trying to aquire lock
Thread-1,failed to aquire

Thread-0,keep lock, left 3 , second 2

// thread2 'll wait to aquire the lock (blocking) until thread0 releases it

Thread-2 aquire lock
Thread-0,keep lock, left 2 , second 3
Thread-0,keep lock, left 1 , second 4

// thread0 releases the lock

// thread2 finally acquires the lock
Thread-2 aquired

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s