/** * @see Lock#tryLock() */ public final boolean tryLock() { return tryAcquire(); }
/** * @see Lock#tryLock(long, TimeUnit) */ public final boolean tryLock(final long time, final TimeUnit unit) { // first try to acquire the lock without any blocking if (tryAcquire()) { return true; } return tryAcquire(unit.toMillis(time)); }
/** * Attempts to acquire in exclusive mode. This method should query if the * state of the object permits it to be acquired in the exclusive mode, and if * so to acquire it. * * <p> * This method is always invoked by the thread performing acquire. If this * method reports failure, the acquire method may queue the thread, if it is * not already queued, until it is signalled by a release from some other * thread. * * <p> * All arguments to this method are ignored and are only there to satisfy the * {@link TryLockObject} interface. * * @return 0 if the acquire succeeded and -1 if it failed */ public final int tryAcquire(int arg, Object ownerId, Object context) { return tryAcquire() ? 0 : -1; }
/** * @see Lock#lock() */ public final void lock() { for (;;) { // first try to acquire the lock without any blocking if (tryAcquire() || tryAcquire(getMaxMillis())) { break; } } }
/** * @see Lock#lockInterruptibly() */ public final void lockInterruptibly() throws InterruptedException { for (;;) { if (tryAcquire()) { return; } if (!tryAcquire(getMaxMillis()) && Thread.interrupted()) { Thread.currentThread().interrupt(); throw new InterruptedException(); } } }
/** * An attempt to acquire lock should succeed when the lock has not been * already taken, either by this thread or another. * * @param msecs * The timeout passed to the {@link LockingPolicy#getTimeout} method. * A value equal to zero means not to wait at all, while a value less * than zero means to wait indefinitely. * * @return - true if lock acquired successfully, false otherwise */ public final boolean attemptLock(long msecs) { // first try to acquire the lock without any blocking if (tryAcquire()) { return true; } if (msecs == 0) { // return the result immediately for zero timeout return false; } if (msecs < 0) { msecs = getMaxMillis(); } return tryAcquire(msecs); }