Java多线程系列(十二)Lock ,Condition

一 Lock

先看下源码注释

{@code Lock} implementations provide more extensive locking
* operations than can be obtained using {@code synchronized} methods
* and statements.  

意思是lock比synchronized提供了更多的操作。

看下方法

1.lock

Acquires the lock.

获取锁

 

2.void lockInterruptibly() throws InterruptedException;

Acquires the lock unless the current thread is
* {@linkplain Thread#interrupt interrupted}.
Acquires the lock unless the current thread is
* {@linkplain Thread#interrupt interrupted}.

 

3.boolean tryLock();   即时return  不停留

4.boolean tryLock(long time, TimeUnit unit) throws InterruptedException;

等待(time unit)还未获取锁则返回

 

5.unlock  Releases the lock.  释放锁

6 Condition newCondition();

* Returns a new {@link Condition} instance that is bound to this
* {@code Lock} instance.

二 Condition

@code Condition} factors out the {@code Object} monitor
* methods ({@link Object#wait() wait}, {@link Object#notify notify}
* and {@link Object#notifyAll notifyAll}) into distinct objects to
* give the effect of having multiple wait-sets per object, by
* combining them with the use of arbitrary {@link Lock} implementations.

  翻译:析出 wait,notify  notifyAll 方法。  可以有多个wait-sets的效果

 

* Where a {@code Lock} replaces the use of {@code synchronized} methods * and statements, a {@code Condition} replaces the use of the Object * monitor methods.

翻译:Lock 取代  synchronize   Condition 取代monitor方法

1 await方法

相当于wait方法

2.void awaitUninterruptibly();

和上面方法包不同在于不会被interrupt

3.long awaitNanos(long nanosTimeout) throws InterruptedException;

等待固定时间

4.boolean await(long time, TimeUnit unit) throws InterruptedException;

等待固定时间和上一个方法的区别在于单位不同

5.boolean awaitUntil(Date deadline) throws InterruptedException;

指定等待到某一时刻

6.signal

Wakes up one waiting thread.

7.signalAll   

Wakes up all waiting threads.

 

 

你可能感兴趣的:(安卓)