Hi All,
I have a simple method which is called from multiple threads;
@Override
public Bitmap getFullBitmap(Filter f, ProgressCallback<String> pc) {
// Requires synchronisation?
Bitmap bitmap = fullMap.get(f.id);
if(bitmap == null){
f.setProgressCallback(pc);
bitmap = f.e.evaluate(currentBitmap);
fullMap.put(f.id, bitmap);
}
return bitmap;
}
As none of the objects used are fields of the class (Apart from fullMap) is it ok just to call this or might one thread change the value of bitmap for example, while the method is executing?
fullMap is a SoftHashMap which maintains SoftReferences of Bitmap objects indexed but the Filter's id that was used to create it. If that makes any sense.
I haven't had any problems but I thought I might need it.
Please ask for clarification if this is not clear, the question makes sense in my head ;)
EDIT
- currentBitmap is an object of type Bitmap, there is one bitmap in the system which is considered current and it's managed by this class.
- This code forms a very basic cache, the bitmap returned will always be the same for each id and is not modified outside of this method.
- By using Soft References in a SoftHashMap as descibed by Dr Heinx and a FIFO queue of hard references for the 10 most recently added I hope to avoid expensive calls to f.e.evaluate. That being said, the call f.e.evaluate will return an identical bitmap object if it is given the same input. After some thought it seems that synchronizing the method is a good idea as nothing positive comes of two threads executing this code for the same filter.
- In addition I made bitmap final as it shouldn't be mutated after creation.
Many thanks! Gav