I have a class like the below and am wondering, will this be thread-safe or can the main thread and the Loader thread possibly have their own copys of the mCache and therefore the get(..) method fail to retreive anything from the cache as it was added in the loader thread? Do i need to mark this volatile?
Thanks!!
public class StackExample
{
private final ConcurrentHashMap<String, SoftReference<Bitmap>> mCache = new ConcurrentHashMap<String, SoftReference<Bitmap>>();
private addToCache(String key, Bitmap bitmap)
{
mCache.put(key, bitmap);
}
private Bitmap getBitmap(String key)
{
if(mCache.contains(key))
{
return mCache.get(key);
}
else
{
//add to loading queue
}
}
private class Loader extends Thread
{
@Override
public void run()
{
...//check loading queue and load some images here
mCache.put(keyString, new SoftReference<Bitmap>(loadedBitmap));
}
}
}