Suppose I have a Java GUI which shows in a panel 40 Batch objects from a selected zone which can go from A to Z.
The 40 Batch objects are queried from database which caches them by zone so that each request for a zone doesn't involve the database every time.
public class BatchView
{
private int drawIt(Graphics g, String zone)
{
for(int i = 0; i<40; i++)
{
Batch tmpBatch = BatchDAO.getBatch(zone, i);
//draw the tmpBatch object
}
}
}
public class BatchDAO
{
public static MyCache cache = new MyCache(5000);
public getAllBatches(String zone)
{
ArrayList<Batch> batchArrayList = cache.get("batch-" + zone);
if(batchArrayList == null)
{
BuildBatchSwingWorker buildBatchSwingWorker = new BuildBatchSwingWorker(zone);
buildBatchSwingWorker.execute();
}
return batchList;
}
public Batch getBatch(String zone, int id)
{
//here I don't query database but exploit the cache
ArrayList<Batch> batchArrayList = getAllBatches(String zone);
for(int i = 0; i< batchArrayList.size(); i++)
{
if(batchArrayList.get(i).getId() == i)
return batchArrayList.get(i);
}
//if batch is not found it means it hasn't loaded yet so I return null
return null;
}
}
Suppose the cache is correcly updated with a series of notifications and each time the drawIt() method correctly updates, how can I make it so that BuildBatchSwingWorker is not called multiple times concurrently for the same zone?