2011-08-17 7 views
11

새로운 Android 클래스 LruCache은 안전합니까? 자바 의사는 다음과 같이 말합니다 :Android LruCache (Android 3.1) 스레드 안전

이 클래스는 스레드로부터 안전합니다. 원자 캐시에 동기화하여 여러 캐시 작업을 수행

synchronized (cache) { 
    if (cache.get(key) == null) { 
     cache.put(key, value); 

    }} 

그들이 스레드 안전하지 말 찾으셨습니까? 클래스가 스레드로부터 안전하면 왜 동기화해야합니까?

감사합니다.

답변

17

클래스가 스레드로부터 안전한지 여부는 중요하지 않습니다. 여러 작업을 사용하는 경우 여전히 동기화가 필요할 수 있습니다. 어떻게 사용 하느냐에 달려 있습니다.

if (cache.get(key) == null) 
{ 
    //at this point you think there is no such value in the cache 
    //but another thread might have just added one between executing 
    //those two lines of code 
    cache.put(key, value); 
}