Java多线程系列--“JUC锁”05之 非公平锁(五)

2014-11-24 03:00:33 · 作者: · 浏览: 3
ent
367 * thread; and {@code false} otherwise
368 */
369 public boolean tryLock() {
370 return sync.nonfairTryAcquire(1);
371 }
372
373 /**
374 * Acquires the lock if it is not held by another thread within the given
375 * waiting time and the current thread has not been
376 * {@linkplain Thread#interrupt interrupted}.
377 *
378 *

Acquires the lock if it is not held by another thread and returns

379 * immediately with the value {@code true}, setting the lock hold count
380 * to one. If this lock has been set to use a fair ordering policy then
381 * an available lock will not be acquired if any other threads
382 * are waiting for the lock. This is in contrast to the {@link #tryLock()}
383 * method. If you want a timed {@code tryLock} that does permit barging on
384 * a fair lock then combine the timed and un-timed forms together:
385 *
386 *
if (lock.tryLock() || lock.tryLock(timeout, unit) ) { ... }
 
387 *
388 *
389 *

If the current thread

390 * already holds this lock then the hold count is incremented by one and
391 * the method returns {@code true}.
392 *
393 *

If the lock is held by another thread then the

394 * current thread becomes disabled for thread scheduling
395 * purposes and lies dormant until one of three things happens:
396 *
397 *
398 *
399 *
  • The lock is acquired by the current thread; or
  • 400 *
    401 *
  • Some other thread {@linkplain Thread#interrupt interrupts}
  • 402 * the current thread; or
    403 *
    404 *
  • The specified waiting time elapses
  • 405 *
    406 *
    407 *
    408 *

    If the lock is acquired then the value {@code true} is returned and

    409 * the lock hold count is set to one.
    410 *
    411 *

    If the current thread:

    412 *
    413 *
    414 *
    415 *
  • has its interrupted status set on entry to this method; or
  • 416 *
    417 *
  • is {@linkplain Thread#interrupt interrupted} while
  • 418 * acquiring the lock,
    419 *
    420 *
    421 * then {@link InterruptedException} is thrown and the current thread's
    422 * interrupted status is cleared.
    423 *
    424 *

    If the specified waiting time elapses then the value {@code false}

    425 * is returned. If the time is less than or equal to zero, the method
    426 * will not wait at all.
    427 *
    428 *

    In this implementation, as this method is an explicit

    429 * interruption point, preference is given to responding to the
    430 * interrupt over normal or reentrant acquisition of the lock, and
    431 * over reporting the elapse of the waiting time.
    432 *
    433 * @param timeout the time to wait for the lock
    434 * @param unit the time unit of the timeout argument
    435 * @return {@code true} if the lock was free and was acquired by the
    436 * current thread, or the lock was already held by the current
    437 * thread; and {@code false} if the waiting time elapsed before
    438 * the lock could be acquired
    439 * @throws InterruptedException if the current thread is interrupted
    440 * @throws NullPointerException if the time unit is null
    441 *
    442 */
    443 public boolean tryLock(long timeout, TimeUnit unit)
    444 throws InterruptedException {
    445 return sync.tryAcquireNanos(1, unit.toNanos(timeout));
    446 }
    447
    448 /**
    449 * Attempts to release this lo