ReentrankLock 分为 非公平锁及公平锁
首先我们看一下它里面有哪些属性:
private final Sync sync;
Sync 这个类是 ReentrantLock的 一个静态内部类,实现了AbstractQueuedSynchronizer
ReentrantLock根据传入构造方法的布尔型参数实例化出Sync的实现类FairSync和NonfairSync,分别表示公平的Sync和非公平的Sync
AbstractQueuedSynchronizer这个类中封装了同步过程中堵塞的线程队列,它里面对线程之间的切换,同步的占用,做了很好的处理
像CountDownLatch、FutureTask、Semaphore、ReentrantLock等都有一个内部类是这个抽象类的子类
下面我们介绍几个ReentrantLock的核心方法及属性:
private volatile int state;
这个属性初始化为0,表示当前没有加锁,每次lock()操作,便会累加这个值,同样一个线程想要获取这个锁,做CAS操作的时候,也是判断state的值是否为0
1
2
3
4
5
6
|
NonfairSync锁: final
void
lock
() {
if
(compareAndSetState(0, 1))
setExclusiveOwnerThread(Thread.currentThread());
else
acquire(1);
}
|
1
|
fairSync锁:
|
final void lock() { acquire(1);}
tryAcquire方法中,先 判断当前state值是否是0,若为0 ,则可进行尝试加锁。若不为0,判断作这个加锁操作的是否为当前线程,若为当前线程,则直接累加,然后更新state值
setExclusiveOwnerThread(current) 是设置当前线程为占有线程
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
fairSync锁:
protected
final boolean tryAcquire(
int
acquires) {
final Thread current = Thread.currentThread();
int
c = getState();
if
(c == 0) {
if
(!hasQueuedPredecessors() &&
compareAndSetState(0, acquires)) {
setExclusiveOwnerThread(current);
return
true
;
}
}
else
if
(current == getExclusiveOwnerThread()) {
int
nextc = c + acquires;
if
(nextc < 0)
throw
new
Error(
"Maximum lock count exceeded"
);
setState(nextc);
return
true
;
}
return
false
;
}
|
1
|
<br>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
NonfairSync锁:final boolean nonfairTryAcquire(
int
acquires) {
final Thread current = Thread.currentThread();
int
c = getState();
if
(c == 0) {
if
(compareAndSetState(0, acquires)) {
setExclusiveOwnerThread(current);
return
true
;
}
}
else
if
(current == getExclusiveOwnerThread()) {
int
nextc = c + acquires;
if
(nextc < 0)
// overflow
throw
new
Error(
"Maximum lock count exceeded"
);
setState(nextc);
return
true
;
}
return
false
;
}
|
同样,unlock(),则会在这个值 上进行-1。从代码上看最终会调用tryRelease 方法,我们可以看到之后在state值为0的时候,才会清空当前占有线程。
当state为0时,tryRelease 返回的则是true, 则会判断head是否为null(不为空则表示有阻塞线程,这里阻塞队列这里怎么处理,可以看AbstractQueuedSynchronizer的处理,参考:http://www.cnblogs.com/xrq730/p/4979021.html)
之后,会进行unparkSuccessor()的处理,这个方法是AbstractQueuedSynchronizer的方法最终会调用LockSupport.unpark(s.thread);s.thread 为阻塞队列中的一个线程,应该是最接近head的阻塞线程(head为阻塞队列的头指针)
1
2
3
|
public
void
unlock() {
sync.release(1);
}
|
1
2
3
4
5
6
7
8
9
|
public
final boolean release(
int
arg) {
if
(tryRelease(arg)) {
Node h = head;
if
(h !=
null
&& h.waitStatus != 0)
unparkSuccessor(h);
return
true
;
}
return
false
;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
|
protected
final boolean tryRelease(
int
releases) {
int
c = getState() - releases;
if
(Thread.currentThread() != getExclusiveOwnerThread())
throw
new
IllegalMonitorStateException();
boolean free =
false
;
if
(c == 0) {
free =
true
;
setExclusiveOwnerThread(
null
);
}
setState(c);
return
free;
}
|
在看JDK源代码的时候,我们会经常的看到try开头的方法,这些方法都是无锁的方式,通过CAS进行多次尝试,会自旋式的获取锁,只有在一定时间内,仍没有获取到锁,才会进行park操作(unsafe的park操作,将线程挂起)
final Thread getOwner()
获取当前占有线程
final int getHoldCount()
获取当前的state值
public final int getQueueLength()
获取阻塞队列的长度
最后,附上简单的使用代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
public
class
ReentrantLockDemo {
private
static
int
i=0;
public
static
void
main(String[] args) throws InterruptedException {
final ReentrantLock myLock =
new
ReentrantLock();
Thread thread1=
new
Thread(
new
Runnable() {
@Override
public
void
run() {
for
(
int
j=0;j<1000;j++){
myLock.
lock
();
i++;
try
{
Thread.sleep(1);
}
catch
(InterruptedException e) {
e.printStackTrace();
}
myLock.unlock();
}
}
});
Thread thread2=
new
Thread(
new
Runnable() {
@Override
public
void
run() {
for
(
int
j=0;j<1000;j++){
myLock.
lock
();
i++;
try
{
Thread.sleep(1);
}
catch
(InterruptedException e) {
e.printStackTrace();
}
myLock.unlock();
}
}
});
thread1.start();
thread2.start();
thread1.
join
();
thread2.
join
();
System.
out
.println(i);
}
}
|
http://bbs.open.qq.com/thread-75651845-1-1.html
http://bbs.open.qq.com/thread-75652089-1-1.htmlhttp://bbs.open.qq.com/thread-75652334-1-1.html
http://bbs.open.qq.com/thread-75652443-1-1.html
http://bbs.open.qq.com/thread-75652666-1-1.html
http://bbs.open.qq.com/thread-75652898-1-1.html
http://bbs.open.qq.com/thread-75653142-1-1.html
http://bbs.open.qq.com/thread-75653392-1-1.html
http://bbs.open.qq.com/thread-75653612-1-1.html
http://bbs.open.qq.com/thread-75653831-1-1.html
http://bbs.open.qq.com/thread-75654342-1-1.html
http://bbs.open.qq.com/thread-75654609-1-1.html
http://bbs.open.qq.com/thread-75655196-1-1.html
http://bbs.open.qq.com/thread-75655781-1-1.html
http://bbs.open.qq.com/thread-75656012-1-1.html
http://bbs.open.qq.com/thread-75656275-1-1.html
http://bbs.open.qq.com/thread-75656781-1-1.html
http://bbs.open.qq.com/thread-75657037-1-1.html
http://bbs.open.qq.com/thread-75657569-1-1.html
http://bbs.open.qq.com/thread-75657859-1-1.html
http://bbs.open.qq.com/thread-75658150-1-1.html
http://bbs.open.qq.com/thread-75658437-1-1.html
http://bbs.open.qq.com/thread-75658687-1-1.html
http://bbs.open.qq.com/thread-75658941-1-1.html
http://bbs.open.qq.com/thread-75659455-1-1.html
http://bbs.open.qq.com/thread-75659768-1-1.html
http://bbs.open.qq.com/thread-75660026-1-1.html
http://bbs.open.qq.com/thread-75660294-1-1.html
http://bbs.open.qq.com/thread-75660525-1-1.html
http://bbs.open.qq.com/thread-75660792-1-1.html
http://bbs.open.qq.com/thread-75661017-1-1.html
http://bbs.open.qq.com/thread-75661439-1-1.html
http://bbs.open.qq.com/thread-75661662-1-1.html
http://bbs.open.qq.com/thread-75661878-1-1.html
http://bbs.open.qq.com/thread-75662143-1-1.html
http://bbs.open.qq.com/thread-75662392-1-1.html
http://bbs.open.qq.com/thread-75662859-1-1.html
http://bbs.open.qq.com/thread-75663373-1-1.html
http://bbs.open.qq.com/thread-75663622-1-1.html
http://bbs.open.qq.com/thread-75664363-1-1.html
http://bbs.open.qq.com/thread-75664850-1-1.html
http://bbs.open.qq.com/thread-75665139-1-1.html
http://bbs.open.qq.com/thread-75665390-1-1.html
http://bbs.open.qq.com/thread-75665952-1-1.html
http://bbs.open.qq.com/thread-75666214-1-1.html
http://bbs.open.qq.com/thread-75666445-1-1.html
http://bbs.open.qq.com/thread-75666677-1-1.html
http://bbs.open.qq.com/thread-75666940-1-1.html
http://bbs.open.qq.com/thread-75667507-1-1.html
http://bbs.open.qq.com/thread-75668064-1-1.html
http://bbs.open.qq.com/thread-75668342-1-1.html
http://bbs.open.qq.com/thread-75668638-1-1.html
http://bbs.open.qq.com/thread-75669191-1-1.html
http://bbs.open.qq.com/thread-75669480-1-1.html
http://bbs.open.qq.com/thread-75669762-1-1.html
http://bbs.open.qq.com/thread-75670042-1-1.html
http://bbs.open.qq.com/thread-75670321-1-1.html
http://bbs.open.qq.com/thread-75670623-1-1.html
http://bbs.open.qq.com/thread-75671165-1-1.html
http://bbs.open.qq.com/thread-75671456-1-1.html
http://bbs.open.qq.com/thread-75671728-1-1.html
http://bbs.open.qq.com/thread-75671995-1-1.html
http://bbs.open.qq.com/thread-75672621-1-1.html
http://bbs.open.qq.com/thread-75672921-1-1.html
http://bbs.open.qq.com/thread-75673493-1-1.html
http://bbs.open.qq.com/thread-75674046-1-1.html
http://bbs.open.qq.com/thread-75674316-1-1.html
http://bbs.open.qq.com/thread-75674542-1-1.html
http://bbs.open.qq.com/thread-75674813-1-1.html
http://bbs.open.qq.com/thread-75675083-1-1.html
http://bbs.open.qq.com/thread-75676124-1-1.html
http://bbs.open.qq.com/thread-75676672-1-1.html
http://bbs.open.qq.com/thread-75676904-1-1.html
http://bbs.open.qq.com/thread-75677423-1-1.html
http://bbs.open.qq.com/thread-75677680-1-1.html
http://bbs.open.qq.com/thread-75678194-1-1.html
http://bbs.open.qq.com/thread-75678689-1-1.html
http://bbs.open.qq.com/thread-75678932-1-1.html
http://bbs.open.qq.com/thread-75679187-1-1.html
http://bbs.open.qq.com/thread-75679389-1-1.html
http://bbs.open.qq.com/thread-75679626-1-1.html
http://bbs.open.qq.com/thread-75679814-1-1.html
http://bbs.open.qq.com/thread-75680095-1-1.html
http://bbs.open.qq.com/thread-75680316-1-1.html
http://bbs.open.qq.com/thread-75680569-1-1.html
http://bbs.open.qq.com/thread-75680847-1-1.html
http://bbs.open.qq.com/thread-75681093-1-1.html
http://bbs.open.qq.com/thread-75681343-1-1.html
http://bbs.open.qq.com/thread-75681598-1-1.html
http://bbs.open.qq.com/thread-75682064-1-1.html
http://bbs.open.qq.com/thread-75682300-1-1.html
http://bbs.open.qq.com/thread-75682548-1-1.html
http://bbs.open.qq.com/thread-75682817-1-1.html
http://bbs.open.qq.com/thread-75683069-1-1.html
http://bbs.open.qq.com/thread-75683561-1-1.html
http://bbs.open.qq.com/thread-75683836-1-1.html
http://bbs.open.qq.com/thread-75684029-1-1.html
http://bbs.open.qq.com/thread-75684665-1-1.html
http://bbs.open.qq.com/thread-75684909-1-1.html
http://bbs.open.qq.com/thread-75685190-1-1.html
http://bbs.open.qq.com/thread-75685475-1-1.html
http://bbs.open.qq.com/thread-75686012-1-1.html
http://bbs.open.qq.com/thread-75686414-1-1.html
http://bbs.open.qq.com/thread-75686607-1-1.html
http://bbs.open.qq.com/thread-75686843-1-1.html
http://bbs.open.qq.com/thread-75687273-1-1.html
http://bbs.open.qq.com/thread-75687519-1-1.html
http://bbs.open.qq.com/thread-75687749-1-1.html
http://bbs.open.qq.com/thread-75688005-1-1.html
http://bbs.open.qq.com/thread-75688274-1-1.html
http://bbs.open.qq.com/thread-75688525-1-1.html
http://bbs.open.qq.com/thread-75688738-1-1.html
http://bbs.open.qq.com/thread-75688953-1-1.html
http://bbs.open.qq.com/thread-75689180-1-1.html
http://bbs.open.qq.com/thread-75689417-1-1.html
http://bbs.open.qq.com/thread-75689637-1-1.html
http://bbs.open.qq.com/thread-75689884-1-1.html
http://bbs.open.qq.com/thread-75690117-1-1.html
http://bbs.open.qq.com/thread-75690368-1-1.html
http://bbs.open.qq.com/thread-75690602-1-1.html
http://bbs.open.qq.com/thread-75690845-1-1.html
http://bbs.open.qq.com/thread-75691065-1-1.html
http://bbs.open.qq.com/thread-75691323-1-1.html
http://bbs.open.qq.com/thread-75691480-1-1.html
http://bbs.open.qq.com/thread-75691970-1-1.html
http://bbs.open.qq.com/thread-75692213-1-1.html
http://bbs.open.qq.com/thread-75692464-1-1.html
http://bbs.open.qq.com/thread-75693193-1-1.html
http://bbs.open.qq.com/thread-75693427-1-1.html
http://bbs.open.qq.com/thread-75693636-1-1.html
http://bbs.open.qq.com/thread-75693913-1-1.html
http://bbs.open.qq.com/thread-75694120-1-1.html
http://bbs.open.qq.com/thread-75694581-1-1.html
http://bbs.open.qq.com/thread-75694813-1-1.html
http://bbs.open.qq.com/thread-75695042-1-1.html
http://bbs.open.qq.com/thread-75695284-1-1.html
http://bbs.open.qq.com/thread-75695520-1-1.html
http://bbs.open.qq.com/thread-75695765-1-1.html
http://bbs.open.qq.com/thread-75696015-1-1.html
http://bbs.open.qq.com/thread-75696220-1-1.html
http://bbs.open.qq.com/thread-75696580-1-1.html
http://bbs.open.qq.com/thread-75696765-1-1.html
http://bbs.open.qq.com/thread-75696984-1-1.html
http://bbs.open.qq.com/thread-75697206-1-1.html
http://bbs.open.qq.com/thread-75697710-1-1.html
http://bbs.open.qq.com/thread-75697962-1-1.html
http://bbs.open.qq.com/thread-75698608-1-1.html
http://bbs.open.qq.com/thread-75698857-1-1.html
http://bbs.open.qq.com/thread-75699102-1-1.html
http://bbs.open.qq.com/thread-75699804-1-1.html
http://bbs.open.qq.com/thread-75700027-1-1.html
http://bbs.open.qq.com/thread-75700515-1-1.html
http://bbs.open.qq.com/thread-75700728-1-1.html
http://bbs.open.qq.com/thread-75700930-1-1.html
http://bbs.open.qq.com/thread-75701279-1-1.html
http://bbs.open.qq.com/thread-75701678-1-1.html
http://bbs.open.qq.com/thread-75701926-1-1.html
http://bbs.open.qq.com/thread-75702172-1-1.html
http://bbs.open.qq.com/thread-75702426-1-1.html
http://bbs.open.qq.com/thread-75702680-1-1.html
http://bbs.open.qq.com/thread-75702907-1-1.html
http://bbs.open.qq.com/thread-75703119-1-1.html
http://bbs.open.qq.com/thread-75704023-1-1.html
http://bbs.open.qq.com/thread-75704493-1-1.html
http://bbs.open.qq.com/thread-75704715-1-1.html
http://bbs.open.qq.com/thread-75704941-1-1.html
http://bbs.open.qq.com/thread-75705143-1-1.html
http://bbs.open.qq.com/thread-75705554-1-1.html
http://bbs.open.qq.com/thread-75705721-1-1.html
http://bbs.open.qq.com/thread-75705911-1-1.html
http://bbs.open.qq.com/thread-75706541-1-1.html
http://bbs.open.qq.com/thread-75706743-1-1.html
http://bbs.open.qq.com/thread-75706965-1-1.html
http://bbs.open.qq.com/thread-75707175-1-1.html
http://bbs.open.qq.com/thread-75707393-1-1.html
http://bbs.open.qq.com/thread-75707818-1-1.html
http://bbs.open.qq.com/thread-75707983-1-1.html
http://bbs.open.qq.com/thread-75708331-1-1.html
http://bbs.open.qq.com/thread-75708482-1-1.html
http://bbs.open.qq.com/thread-75708812-1-1.html
http://bbs.open.qq.com/thread-75709019-1-1.html
http://bbs.open.qq.com/thread-75709350-1-1.html
http://bbs.open.qq.com/thread-75709544-1-1.html
http://bbs.open.qq.com/thread-75709713-1-1.html
http://bbs.open.qq.com/thread-75709927-1-1.html
http://bbs.open.qq.com/thread-75710374-1-1.html
http://bbs.open.qq.com/thread-75710592-1-1.html
http://bbs.open.qq.com/thread-75710811-1-1.html
http://bbs.open.qq.com/thread-75711043-1-1.html
http://bbs.open.qq.com/thread-75711224-1-1.html
http://bbs.open.qq.com/thread-75711433-1-1.html
http://bbs.open.qq.com/thread-75711619-1-1.html
http://bbs.open.qq.com/thread-75711773-1-1.html
http://bbs.open.qq.com/thread-75711941-1-1.html
http://bbs.open.qq.com/thread-75712104-1-1.html
http://bbs.open.qq.com/thread-75712299-1-1.html
http://bbs.open.qq.com/thread-75712535-1-1.html
http://bbs.open.qq.com/thread-75712750-1-1.html
http://bbs.open.qq.com/thread-75713150-1-1.html
http://bbs.open.qq.com/thread-75713368-1-1.html
http://bbs.open.qq.com/thread-75713555-1-1.html
http://bbs.open.qq.com/thread-75714026-1-1.html
http://bbs.open.qq.com/thread-75714294-1-1.html
http://bbs.open.qq.com/thread-75714518-1-1.html
http://bbs.open.qq.com/thread-75715004-1-1.html
http://bbs.open.qq.com/thread-75715217-1-1.html
http://bbs.open.qq.com/thread-75715594-1-1.html
http://bbs.open.qq.com/thread-75715836-1-1.html
http://bbs.open.qq.com/thread-75716312-1-1.html
http://bbs.open.qq.com/thread-75716549-1-1.html
http://bbs.open.qq.com/thread-75717012-1-1.html
http://bbs.open.qq.com/thread-75717232-1-1.html
http://bbs.open.qq.com/thread-75717690-1-1.html
http://bbs.open.qq.com/thread-75717879-1-1.html
http://bbs.open.qq.com/thread-75718394-1-1.html
http://bbs.open.qq.com/thread-75718634-1-1.html
http://bbs.open.qq.com/thread-75719103-1-1.html
http://bbs.open.qq.com/thread-75719547-1-1.html
http://bbs.open.qq.com/thread-75719778-1-1.html
http://bbs.open.qq.com/thread-75720325-1-1.html
http://bbs.open.qq.com/thread-75720535-1-1.html
http://bbs.open.qq.com/thread-75720760-1-1.html
http://bbs.open.qq.com/thread-75720982-1-1.html
http://bbs.open.qq.com/thread-75721194-1-1.html
http://bbs.open.qq.com/thread-75721404-1-1.html
http://bbs.open.qq.com/thread-75721847-1-1.html
http://bbs.open.qq.com/thread-75722083-1-1.html
http://bbs.open.qq.com/thread-75722297-1-1.html
http://bbs.open.qq.com/thread-75722517-1-1.html
http://bbs.open.qq.com/thread-75722721-1-1.html
http://bbs.open.qq.com/thread-75722916-1-1.html
http://bbs.open.qq.com/thread-75723589-1-1.html
http://bbs.open.qq.com/thread-75723819-1-1.html
http://bbs.open.qq.com/thread-75724069-1-1.html
http://bbs.open.qq.com/thread-75724279-1-1.html
http://bbs.open.qq.com/thread-75724519-1-1.html
http://bbs.open.qq.com/thread-75724721-1-1.html
http://bbs.open.qq.com/thread-75724892-1-1.html
http://bbs.open.qq.com/thread-75725328-1-1.html
http://bbs.open.qq.com/thread-75725724-1-1.html
http://bbs.open.qq.com/thread-75725930-1-1.html
http://bbs.open.qq.com/thread-75726334-1-1.html
http://bbs.open.qq.com/thread-75726576-1-1.html
http://bbs.open.qq.com/thread-75726818-1-1.html
http://bbs.open.qq.com/thread-75727045-1-1.html
http://bbs.open.qq.com/thread-75727554-1-1.html
http://bbs.open.qq.com/thread-75727814-1-1.html
http://bbs.open.qq.com/thread-75728284-1-1.html
http://bbs.open.qq.com/thread-75728534-1-1.html
http://bbs.open.qq.com/thread-75728765-1-1.html