I have a list of intensive updates so I am grouping them together and executing them as a batch job in a single thread. Other threads can send their updates at any time.
class ItemUpdateJob {
int itemId;
int number;
}
When scheduling a job to be queued for updating, I want a collection where I can modify a job if it already exists (assuming itemId as the key). In this example:
existingItemJobInQueue.number += requestedItemJob.number;
so the queue doesn't start having thousands of jobs for the same item. When the jobs begin execution I will need to somehow loop through the queue, but while updating a job, it should not be updated (should each item have its own lock?).
for (ItemUpdateJob job : jobQueue) {
updateItem(job);
}
Once a job has been updated, it should immediately be removed from the queue. What is the best way to do this? Currently I am thinking of using a HashMap with the item id as the key, then each item has a lock which prevents an existing job from being modified while the item is being updated. Although, this will cause a halt as it waits for the update to complete (lock to be released).