Android LruCache(Android 3.1)线程安全

Android LruCache(Android 3.1)线程安全,android,thread-safety,lru,Android,Thread Safety,Lru,新的Android类线程安全吗?java文档说: 这个类是线程安全的。通过在缓存上同步,以原子方式执行多个缓存操作: 他们的意思是说线程不安全吗?如果类是线程安全的,为什么要同步 谢谢 不管类是否是线程安全的。如果使用多个操作,可能仍需要同步。这取决于你如何使用它 if (cache.get(key) == null) { //at this point you think there is no such value in the cache //but another thread

新的Android类线程安全吗?java文档说:

这个类是线程安全的。通过在缓存上同步,以原子方式执行多个缓存操作:

他们的意思是说线程不安全吗?如果类是线程安全的,为什么要同步


谢谢

不管类是否是线程安全的。如果使用多个操作,可能仍需要同步。这取决于你如何使用它

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);
}
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);
}