Java多线程系列--“JUC锁”05之 非公平锁(四)
d by another thread then the
285 * current thread becomes disabled for thread scheduling
286 * purposes and lies dormant until the lock has been acquired,
287 * at which time the lock hold count is set to one.
288 */
289 public void lock() {
290 sync.lock();
291 }
292
293 /**
294 * Acquires the lock unless the current thread is
295 * {@linkplain Thread#interrupt interrupted}.
296 *
297 *
Acquires the lock if it is not held by another thread and returns
298 * immediately, setting the lock hold count to one.
299 *
300 *
If the current thread already holds this lock then the hold count
301 * is incremented by one and the method returns immediately.
302 *
303 *
If the lock is held by another thread then the
304 * current thread becomes disabled for thread scheduling
305 * purposes and lies dormant until one of two things happens:
306 *
307 *
308 *
309 * The lock is acquired by the current thread; or
310 *
311 * Some other thread {@linkplain Thread#interrupt interrupts} the
312 * current thread.
313 *
314 *
315 *
316 *
If the lock is acquired by the current thread then the lock hold
317 * count is set to one.
318 *
319 *
If the current thread:
320 *
321 *
322 *
323 * has its interrupted status set on entry to this method; or
324 *
325 * is {@linkplain Thread#interrupt interrupted} while acquiring
326 * the lock,
327 *
328 *
329 *
330 * then {@link InterruptedException} is thrown and the current thread's
331 * interrupted status is cleared.
332 *
333 *
In this implementation, as this method is an explicit
334 * interruption point, preference is given to responding to the
335 * interrupt over normal or reentrant acquisition of the lock.
336 *
337 * @throws InterruptedException if the current thread is interrupted
338 */
339 public void lockInterruptibly() throws InterruptedException {
340 sync.acquireInterruptibly(1);
341 }
342
343 /**
344 * Acquires the lock only if it is not held by another thread at the time
345 * of invocation.
346 *
347 *
Acquires the lock if it is not held by another thread and
348 * returns immediately with the value {@code true}, setting the
349 * lock hold count to one. Even when this lock has been set to use a
350 * fair ordering policy, a call to {@code tryLock()} will
351 * immediately acquire the lock if it is available, whether or not
352 * other threads are currently waiting for the lock.
353 * This "barging" behavior can be useful in certain
354 * circumstances, even though it breaks fairness. If you want to honor
355 * the fairness setting for this lock, then use
356 * {@link #tryLock(long, TimeUnit) tryLock(0, TimeUnit.SECONDS) }
357 * which is almost equivalent (it also detects interruption).
358 *
359 *
If the current thread already holds this lock then the hold
360 * count is incremented by one and the method returns {@code true}.
361 *
362 *
If the lock is held by another thread then this method will return
363 * immediately with the value {@code false}.
364 *
365 * @return {@code true} if the lock was free and was acquired by the
366 * current thread, or the lock was already held by the curr