1

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
3
  • How many of these creatures are philosophers? :) Perhaps you're best off with a taskmaster thread who assigns the jobs and items to the creatures? Commented Mar 11, 2013 at 22:20
  • 1
    There are a few ways to lock groups of objects. But before we go there, I have a question: what are you trying to achieve with locking? Commented Mar 11, 2013 at 22:23
  • @SlavaImeshev "such that no job can access an item at the same time" if I have 3 bottles and a task is using 2, I want only 1 bottle to be available. Commented Mar 12, 2013 at 13:08

2 Answers 2

1

You should make a list of the concurrent operations (methods of your classes) you want to perform. Outline their dependencies and invariants, so you know what can be done concurrently and what must be serialized (synchronized). Define your jobs as mostly independent tasks to achieve maximum concurrency with minimal blocking. Take a good look at what java.util.concurrent has to offer, especially the higher-level classes like Semaphore, etc.

Sorry if this sounds too broad and general.

Sign up to request clarification or add additional context in comments.

Comments

0

First, as noted by some others, you need to be a bit clearer, and/or analyze, what you are trying to do.

Second, one "easy for the programmer" approach is to use CopyOnWriteArrayLists for anything "tricky". It may not be the most efficient in terms of speed, but, it's likely plenty fast enough for your application. At least give it some thought.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.