0
다음 코드 단편은 libcore 프로젝트의 android JellyBean ReferenceQueue.java에서 가져온 것입니다.정적 동기화 된 메서드와 동기화 된 블록의 모든 코드가있는 정적 메서드
동기화 된 한정자를 메서드에 추가하는 대신 ReferenceQueue.class에서 동기화하여 동기화 된 블록이 사용 된 이유를 알려줄 수 있습니까? 이 두 가지 접근 방식은이 경우 기능적으로 동일합니까?
내가봤을 때 비슷한 방법으로 동기화하는 것이 더 효율적으로 보인다.
환호, 매트
public class ReferenceQueue<T> {
... <snip> ...
public static Reference unenqueued = null;
static void add(Reference<?> list) {
synchronized (ReferenceQueue.class) {
if (unenqueued == null) {
unenqueued = list;
} else {
Reference<?> next = unenqueued.pendingNext;
unenqueued.pendingNext = list.pendingNext;
list.pendingNext = next;
}
ReferenceQueue.class.notifyAll();
}
}
여기에서 좋은 설명과 의견을 찾을 수 있습니다. http://stackoverflow.com/questions/574240/synchronized-block-vs-synchronized-method –