コード例 #1
0
  /**
   * Method "tryLockUnrestricted".
   *
   * @param key
   * @return {@code true} if the lock was free and was acquired by the current thread, or the lock
   *     was already held by the current thread; and {@code false} otherwise
   * @throws InterruptedException
   * @throws IllegalArgumentException if bean is null
   * @see java.util.concurrent.locks.ReentrantLock#tryLock()
   */
  public boolean tryLockUnrestricted(final KP key) {
    checkStopped();
    blockOperationIfRequired();
    incrementRunningOperations();
    boolean tryLockResultBoolean;
    try {
      final AtomicBoolean tryLockResult = new AtomicBoolean();
      final CyclicBarrier cyclicBarrier = new CyclicBarrier(2);
      Callable<Boolean> callable =
          new Callable<Boolean>() {

            /*
             * (non-Javadoc)
             *
             * @see java.util.concurrent.Callable#call()
             */
            @Override
            public Boolean call() throws Exception {
              boolean locked;
              try {
                locked = locker.tryLock(key);
                tryLockResult.set(locked);
              } finally {
                // STEP 1
                cyclicBarrier.await();
              }
              if (locked) {
                // STEP 2
                cyclicBarrier.await();
                return locker.unlock(key);
              } else {
                return false;
              }
            }
          };
      Future<Boolean> futureTask = threadPool.submit(callable);
      try {
        // STEP 1
        cyclicBarrier.await();
      } catch (Exception e) {
        throw new RuntimeException(e);
      }
      tryLockResultBoolean = tryLockResult.get();
      if (tryLockResultBoolean) {
        LockerValue<KP> lockerValue = locker.getLockerValue(key);
        Thread callerThreadLocker = Thread.currentThread();
        lockerValue.addHandler(
            new LockerValueHandler(futureTask, cyclicBarrier, callerThreadLocker));
      }
    } finally {
      decrementRunningOperations();
    }
    return tryLockResultBoolean;
  }
コード例 #2
0
  /**
   * Method "tryLockUnrestricted".
   *
   * @param key
   * @param timeout the time to wait for the lock
   * @param unit the time unit of the timeout argument
   * @return true if the lock was free and was acquired by the current thread, or the lock was
   *     already held by the current thread; and false if the waiting time elapsed before the lock
   *     could be acquired
   * @throws InterruptedException
   * @throws IllegalArgumentException if bean is null
   * @see java.util.concurrent.locks.ReentrantLock#tryLock(long, java.util.concurrent.TimeUnit)
   */
  public boolean tryLockUnrestricted(final KP key, final long timeout, final TimeUnit unit)
      throws InterruptedException {
    checkStopped();
    blockOperationIfRequired();
    incrementRunningOperations();
    boolean tryLockResultBoolean = false;
    try {
      final AtomicBoolean tryLockResult = new AtomicBoolean();
      final AtomicReference<InterruptedException> interruptedExceptionFromTryRef =
          new AtomicReference<InterruptedException>();
      final CyclicBarrier cyclicBarrier = new CyclicBarrier(2);
      Callable<Boolean> callable =
          new Callable<Boolean>() {

            /*
             * (non-Javadoc)
             *
             * @see java.util.concurrent.Callable#call()
             */
            @Override
            public Boolean call() throws Exception {
              boolean locked = false;
              try {
                locked = locker.tryLock(key, timeout, unit);
                tryLockResult.set(locked);
              } catch (InterruptedException e) {
                interruptedExceptionFromTryRef.set(e);
                return false;
              } finally {
                // STEP 1
                cyclicBarrier.await();
              }
              if (locked) {
                // STEP 2
                cyclicBarrier.await();
                return locker.unlock(key);
              } else {
                return false;
              }
            }
          };
      Future<Boolean> futureTask = threadPool.submit(callable);
      try {
        // STEP 1
        cyclicBarrier.await();
      } catch (Exception e) {
        throw new RuntimeException(e);
      }
      InterruptedException interruptedExceptionFromTry = interruptedExceptionFromTryRef.get();
      if (interruptedExceptionFromTry != null) {
        throw interruptedExceptionFromTry;
      }
      tryLockResultBoolean = tryLockResult.get();
      if (tryLockResultBoolean) {
        LockerValue<KP> lockerValue = locker.getLockerValue(key);
        Thread threadLocker = Thread.currentThread();
        lockerValue.addHandler(new LockerValueHandler(futureTask, cyclicBarrier, threadLocker));
        locker.traceStackForDebugging(lockerValue);
      }
    } finally {
      decrementRunningOperations();
    }

    return tryLockResultBoolean;
  }
コード例 #3
0
  /**
   * Method "lockInterruptiblyUnrestricted".
   *
   * @param key
   * @throws InterruptedException
   * @see java.util.concurrent.locks.ReentrantLock#lockInterruptibly()
   */
  public void lockInterruptiblyUnrestricted(final KP key) throws InterruptedException {
    checkStopped();
    blockOperationIfRequired();

    LockerValue<KP> lockerValue = null;
    LockerValueHandler handler = null;

    if (tryLockUnrestricted(key)) {
      return;
    }

    incrementRunningOperations();

    /* Test if already locked by the same thread */
    lockerValue = locker.getLockerValue(key);
    if (locker != null) {
      handler = lockerValue.getHandler();
      if (handler != null && Thread.currentThread() == handler.getCallerThreadLocker()) {
        decrementRunningOperations();
        return;
      }
    }

    try {
      final Thread threadLocker = Thread.currentThread();
      final CyclicBarrier cyclicBarrier = new CyclicBarrier(2);
      final AtomicBoolean hasError = new AtomicBoolean();
      Callable<Boolean> callable =
          new Callable<Boolean>() {

            /*
             * (non-Javadoc)
             *
             * @see java.util.concurrent.Callable#call()
             */
            @Override
            public Boolean call() throws Exception {
              try {
                locker.lockInterruptibly(key);
              } catch (Exception e) {
                hasError.set(true);
                throw e;
              } finally {
                // STEP 1
                cyclicBarrier.await();
              }
              // STEP 2
              cyclicBarrier.await();
              boolean unlocked = locker.unlock(key);
              return unlocked;
            }
          };
      Future<Boolean> futureTask = threadPool.submit(callable);
      try {
        // STEP 1
        cyclicBarrier.await();
      } catch (BrokenBarrierException e) {
        throw new RuntimeException(e);
      }

      if (hasError.get()) {
        try {
          futureTask.get();
        } catch (ExecutionException e) {
          Throwable cause = e.getCause();
          if (cause != null && cause instanceof InterruptedException) {
            throw (InterruptedException) cause;
          } else {
            throw new RuntimeException(e);
          }
        }
      }

      lockerValue = locker.getLockerValue(key);
      lockerValue.addHandler(new LockerValueHandler(futureTask, cyclicBarrier, threadLocker));
    } finally {
      decrementRunningOperations();
    }
  }