开发者

Android LruCache (Android 3.1) thread safety

开发者 https://www.devze.com 2023-03-29 02:16 出处:网络
Is the new Android class LruCache thread safe? The java doc says: This class is thread-safe. Perform multiple cache operations atomically by synchronizing on the cache:

Is the new Android class LruCache thread safe? The java doc says:

This class is thread-safe. Perform multiple cache operations atomically by synchronizing on the cache:

   synchronized (cache) {
     if (cache.get(key) =开发者_StackOverflow中文版= null) {
         cache.put(key, value);

   }}

Did they mean to say NOT thread-safe? Why would one have to synchronize if the class is thread safe?

Thanks!


Doesn't matter whether the class is thread-safe or not. If you use multiple operations you may still need to synchronize. Depends on how you use it.

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号