방금 akka에서 코드 조각을 발견했습니다.AKKA에서 사용되는이 CCAS locking machanizion을 이해하는 방법은 무엇입니까?
내가 아래에 나열된에 관심이 핵심 방법.
/**
* A very simple lock that uses CCAS (Compare Compare-And-Swap)
* Does not keep track of the owner and isn't Reentrant, so don't nest and try to stick to the if*-methods
*/
class SimpleLock {
val acquired = new AtomicBoolean(false)
def ifPossible(perform:() => Unit): Boolean = {
if (tryLock()) {
try {
perform
} finally {
unlock()
}
true
} else false
}
def tryLock() = {
if (acquired.get) false
else acquired.compareAndSet(false, true)
}
def tryUnlock() = {
acquired.compareAndSet(true, false)
}
두 개의 관련 하위 쿼리가 있습니다.
1)의 작동 방식에 대한이 클래스 SimpleLock
2) 어떤 힌트이나 배경 지식의 목적은 무엇입니까?
이 코드는 JAVA와 scala로 작성되었으므로 AtomicBoolean 클래스를 사용합니다. 그래서 자바 태그도 추가합니다.
어떤 조언을 환영합니다! 왜 누군가가이 질문에 투표하는 지 잘 모르겠습니다.
관련 :
Can anyone interpret this C++ code (from OpenJDK6) into plain English?
문제를 해결해야한다고 생각합니다. 당신이 이해하지 못하는 것은 정확히 무엇입니까? –
어떻게 작동하는지에 대한 기본적인 개념이 필요합니다. 고마워요, 선생님 :) –