文章目录
简介内部类非公平锁公平锁
简介
Semaphore信号量,用来控制同时访问特定资源的线程数量,他通过协调各个线程,以保证合理地使用公用资源。
内部类
abstract static class Sync extends AbstractQueuedSynchronizer {}
static final class NonfairSync extends Sync {
private static final long serialVersionUID
= -2694183684443567898L
;
NonfairSync(int permits
) {
super(permits
);
}
protected int tryAcquireShared(int acquires
) {
return nonfairTryAcquireShared(acquires
);
}
}
static final class FairSync extends Sync {
private static final long serialVersionUID
= 2014338818796000944L
;
FairSync(int permits
) {
super(permits
);
}
protected int tryAcquireShared(int acquires
) {
for (;;) {
if (hasQueuedPredecessors())
return -1;
int available
= getState();
int remaining
= available
- acquires
;
if (remaining
< 0 ||
compareAndSetState(available
, remaining
))
return remaining
;
}
}
}
非公平锁
Semaphore semaphore
= new Semaphore(10);
semaphore
.acquire();
public void acquire() throws InterruptedException
{
sync
.acquireSharedInterruptibly(1);
}
public final void acquireSharedInterruptibly(int arg
)
throws InterruptedException
{
if (Thread
.interrupted())
throw new InterruptedException();
if (tryAcquireShared(arg
) < 0)
doAcquireSharedInterruptibly(arg
);
}
protected int tryAcquireShared(int acquires
) {
return nonfairTryAcquireShared(acquires
);
}
final int nonfairTryAcquireShared(int acquires
) {
for (;;) {
int available
= getState();
int remaining
= available
- acquires
;
if (remaining
< 0 ||
compareAndSetState(available
, remaining
))
return remaining
;
}
}
private void doAcquireSharedInterruptibly(int arg
)
throws InterruptedException
{
final Node node
= addWaiter(Node
.SHARED
);
boolean failed
= true;
try {
for (;;) {
final Node p
= node
.predecessor();
if (p
== head
) {
int r
= tryAcquireShared(arg
);
if (r
>= 0) {
setHeadAndPropagate(node
, r
);
p
.next
= null
;
failed
= false;
return;
}
}
if (shouldParkAfterFailedAcquire(p
, node
) &&
parkAndCheckInterrupt())
throw new InterruptedException();
}
} finally {
if (failed
)
cancelAcquire(node
);
}
}
semaphore
.release();
public void release() {
sync
.releaseShared(1);
}
public final boolean releaseShared(int arg
) {
if (tryReleaseShared(arg
)) {
doReleaseShared();
return true;
}
return false;
}
protected final boolean tryReleaseShared(int releases
) {
for (;;) {
int current
= getState();
int next
= current
+ releases
;
if (next
< current
)
throw new Error("Maximum permit count exceeded");
if (compareAndSetState(current
, next
))
return true;
}
}
公平锁
Semaphore semaphore
= new Semaphore(10,true);
semaphore
.acquire();
public void acquire() throws InterruptedException
{
sync
.acquireSharedInterruptibly(1);
}
public final void acquireSharedInterruptibly(int arg
)
throws InterruptedException
{
if (Thread
.interrupted())
throw new InterruptedException();
if (tryAcquireShared(arg
) < 0)
doAcquireSharedInterruptibly(arg
);
}
protected int tryAcquireShared(int acquires
) {
for (;;) {
if (hasQueuedPredecessors())
return -1;
int available
= getState();
int remaining
= available
- acquires
;
if (remaining
< 0 ||
compareAndSetState(available
, remaining
))
return remaining
;
}
}
semaphore
.release();
转载请注明原文地址: https://mac.8miu.com/read-514298.html