锁是实现多线程同步的一个重要手段,在之前的代码中也看到了它的强大作用,看了代码,Lock是一个接口,它有几个其它的实现类,具体的在后面会分析到,有人就会说它与同步关键字synchronized的区别有什么呢?我们还是看看java doc是怎么说的。A lock is a tool for controlling access to a shared resource by multiple threads. Commonly, a lock provides exclusive access to a shared resource: only one thread at a time can acquire the lock and all access to the shared resource requires that the lock be acquired first. However, some locks may allow concurrent access to a shared resource, such as the read lock of aReadWriteLock.
The use of synchronized methods or statements provides access to the implicit monitor lock associated with every object, but forces all lock acquisition and release to occur in a block-structured way: when multiple locks are acquired they must be released in the opposite order, and all locks must be released in the same lexical scope in which they were acquired.
上面说了一堆,就表明了一个观点Lock的方便性,可以不按顺序的释放锁。当然通过下面提供的方法可以看出,Lock还支持可中断的特性。
Lock接口有以下几个方法:
1.void lock()
看一个javadoc是怎么说的: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.
如果能够获取锁,就返回,如果不能获取,它就会一直在等待获取锁,是不到黄河不死心啊。
2.void lockInterruptibly()
Acquires the lock unless the current thread isinterrupted.
这个是支持线程中断的,如果线程中断,默认它就不会去竞争了,这个相对于同步代码代码与同步方法而言,可扩展性还是大了很多。
3.boolean tryLock()
这个方法很明显,如果能获取锁,就立马返回true,否则就返回false。
4.boolean tryLock(long time, TimeUnit unit)
如果能获锁,就返回true,如果有以下两种情况,就会设置成false:
一是线程被中断了;
二是时间到了。
5.void unlock()
释放一个锁。
6.Condition newCondition()
引入Condition有两个方法的意图:
a.对一个共享资源有读和写的能力,如果读线程或写线程获取了Lock的权力,即有能力进入,但是如果里面没有内容,读也没有用,如果空间已满了,写也写不了,所有还得有条件去判断一下,是不是线程要等待了;
b.提供一种多线程之间的通信机制,类似wait()和nofity()的原理。
上面说到了线程中断,就扯一下,如果一个线程被中断了,即调用了中断的方法,请注意,此时是没有中止线程的运行,只是将它的中断标志位设置成true了。如果一个线程在阻塞状态时,并且它的中断标志位为true时,如果中断该线程,那么它会报一个线程中断的异常出来。
接下面就要分析Lock的实现类了,从常用的ReentrantLock类开吧。