Let's say I have four classes: Party, Creature, Job, Item.
Parties have creatures
Parties have items
Creatures have jobs (Threads)
Jobs require items
I am having trouble determining how to synchronize the individual items, such that no job can access an item at the same time. I assembled a list of all the items available in the party in the form
ArrayList<ItemInstance>
where ItemInstance has an Item and an integer denoting the quantity. I don't know how to perform the locks on this list
ReentrantLock resourceLock = new ReentrantLock();
ArrayList<Condition> resourceConditions = new ArrayList<Condition>();
I thought about doing this, but locking resourceLock will lock all of the Items, correct? Also I'll need to lock a specific amount of an Item. so my array entry might be
(Swords, 4)
in my master list, but I might only need to use 3 swords for my Job. Should I just store them all in a single column and remove the quantity?
The general concept is I have one big Party list with all the items and quantities, then I have lots of Jobs with little lists of Items and quantities needed. I'm trying to make my Jobs threads wait for enough of every item, execute, then release the Items to be reused.
PartyList =
(Swords, 7)
(Forks, 2)
(Bottles, 5)
JobEatList =
(Forks, 1)
//RUN THREAD
PartyList =
(Swords, 7)
(Forks, 1)
(Bottles, 5)
JobDineList =
(Bottles, 2)
(Forks, 4)
//WAIT