■ 前言 - 内存锁的不足
■ lock 接口综述
并发包中的lock接口实现类,其中readlock和writelock是reentrantreadwritelock的静态内部类,如图:
■ lock 接口方法标准使用
//标准用法 lock lock = new reentrantlock(); //获取锁应在try之前,因为若获取锁时发生异常,异常抛出同时会导致锁无故释放 lock.lock(); try{ dosometing(); }finally{ //注意:必须在finally块中释放锁,目的是保证在获取到锁之后,最终能被释放 lock.unlock(); }
■ lock 接口方重点方法
1. lock()
/** * acquires the lock. * 获取锁,调用该方法的当前线程将会获取锁,当锁获得后,从该方法返回 * <p>if the lock is not available then the current thread becomes * disabled for thread scheduling purposes and lies dormant until the * lock has been acquired. * 若当前锁不可用(已被占有),当前线程会一直休眠直到锁为可被获取状态 * <p><b>implementation considerations</b> * 实现该方法的注意事项 * <p>a {@code lock} implementation may be able to detect erroneous use * of the lock, such as an invocation that would cause deadlock, and * may throw an (unchecked) exception in such circumstances. the * circumstances and the exception type must be documented by that * {@code lock} implementation. * 该方法的实现需要能发现lock被错误使用,如死锁或抛出不可查异常(即可运行期异常和error) * 此时该实现必须用文档注明其可能出现的异常或需要的使用环境 */ void lock();
2. lockinterruptibly()
//样例代码
public boolean dotask throws interruptedexception(){ lock.lockinterruptibly(); try{ return canceltask(); }finally{ lock.unlock(); } } //取消任务 private boolean canceltask() throws interruptedexception {...}
/** * acquires the lock unless the current thread is * {@linkplain thread#interrupt interrupted}. * 可中断地获取锁,即在锁的获取中可以中断当前线程 * <p>acquires the lock if it is available and returns immediately. * 当获取锁时锁可用就立即返回 * <p>if the lock is not available then the current thread becomes * disabled for thread scheduling purposes and lies dormant until * one of two things happens: * <ul> * <li>the lock is acquired by the current thread; or * <li>some other thread {@linkplain thread#interrupt interrupts} the * current thread, and interruption of lock acquisition is supported. * </ul> * 若当前锁不可用(已被占有),当前线程会一直休眠直到以下两种情况发生: * 1.锁被当前线程获取 * 2.其他线程中断当前线程,同时锁的获取允许被中断 * <p><b>implementation considerations</b> * 实现该方法的注意事项 * <p>the ability to interrupt a lock acquisition in some * implementations may not be possible, and if possible may be an * expensive operation. the programmer should be aware that this * may be the case. an implementation should document when this is * the case. * 该方法属于拓展方法,只有需要中断服务的时候才需要实现它 * <p>an implementation can favor responding to an interrupt over * normal method return. * 相对于返回,该方法更适合抛出一个中断响应,比如中断异常 * @throws interruptedexception if the current thread is * interrupted while acquiring the lock (and interruption * of lock acquisition is supported) */ void lockinterruptibly() throws interruptedexception;
3. trylock()
/** * acquires the lock only if it is free at the time of invocation. * 尝试非阻塞的获取锁,调用该方法后立即返回是否成功获取锁true/false * <p>acquires the lock if it is available and returns immediately * with the value {@code true}. * if the lock is not available then this method will return * immediately with the value {@code false}. * 当锁不可用时立即返回false * this usage ensures that the lock is unlocked if it was acquired, and * doesn't try to unlock if the lock was not acquired. * 该实现应确保当锁被获取时是未锁状态,当未被获取时不会尝试解锁 * @return {@code true} if the lock was acquired and * {@code false} otherwise */ boolean trylock(); /** * acquires the lock if it is free within the given waiting time and the * current thread has not been {@linkplain thread#interrupt interrupted}. * 没有被中断当前线程在指定超时时间内获取锁 * if the lock is not available then the current thread becomes disabled for * thread scheduling purposes and lies dormant until one of three things happens: * <ul> * <li>the lock is acquired by the current thread; or * <li>some other thread {@linkplain thread#interrupt interrupts} the * current thread, and interruption of lock acquisition is supported; or * <li>the specified waiting time elapses * </ul> * <p>if the specified waiting time elapses then the value {@code false} is returned. * if the time is less than or equal to zero, the method will not wait at all. * 当前线程在以下三种情况下会返回: * 1.当前线程在超时时间内获得锁 * 2.当前线程在超时时间内被中断 * 3.超时时间结束,返回false,线程不再被阻塞 * @param time the maximum time to wait for the lock * @param unit the time unit of the {@code time} argument * @return {@code true} if the lock was acquired and {@code false} * if the waiting time elapsed before the lock was acquired * @throws interruptedexception if the current thread is interrupted * while acquiring the lock (and interruption of lock * acquisition is supported) */ boolean trylock(long time, timeunit unit) throws interruptedexception;
4. unlock()
/** * releases the lock. * 释放锁 */ void unlock();
5. newcondition 方法
/** * returns a new {@link condition} instance that is bound to this * {@code lock} instance. * 返回一个等待通知组(条)件 * <p>before waiting on the condition the lock must be held by the current thread. * a call to {@link condition#await()} will atomically release the lock * before waiting and re-acquire the lock before the wait returns. * 该组件与当前锁绑定,当先线程只有获得锁才能调用该组件的await方法并释放锁 * @return a new {@link condition} instance for this {@code lock} instance * @throws unsupportedoperationexception if this {@code lock} * implementation does not support conditions */ condition newcondition();
如对本文有疑问, 点击进行留言回复!!
【奥鹏作业答案库网】大工20春《应用统计》在线作业1【奥鹏作业答案】
网友评论