标签:
http://my.oschina.net/clopopo/blog/140479
public
class
TASLock {
private
AtomicBoolean state =
new
AtomicBoolean(
false
);
public
void
lock() {
while
(state.getAndSet(
true
)) {
}
}
public
void
unlock() {
state.set(
false
);
}
}
public
class
TTASLock {
private
AtomicBoolean state =
new
AtomicBoolean(
false
);
// 加锁
public
void
lock() {
while
(
true
) {
while
(state.get()) {
// 自旋
}
if
(!state.getAndSet(
true
)) {
break
;
}
}
}
public
void
unlock() {
state.set(
false
);
}
}
标签:
原文地址:http://www.cnblogs.com/WeRtogether/p/4487972.html