On May 5, 11:04 pm, Daniel Pitts
<newsgroup.spamfil...@virtualinfinity.net> wrote:
Philipp wrote:
Hello,
I've come accross a threading problem for which I can't find a nice
solution. (SSCCP at end of post)
I have a bidimensional array of objects (view it as a 2D lattice). I
want to make atomic operations on random square 2x2 regions of the
lattice. Thus I want to lock the 4 objects of the region, then perform
the change, then unlock those objects. Several threads should be
allowed to work on the array at the same time, if each thread is
accessing a 2x2 region which does not overlap with that of another,
they should be capable of doing the work in a parallel way.
How should I design the code to achieve this?
Here is the design I would consider:
Note, this is *completely* untested. It is also just a first iteration..
Replacing "boolean[][] locks" with a BitSet may be advantageous,
depending on the size of your data set.
Your approach is very nice. It is reusable and flexible, particularly
through the use of the Operator interface and generics. The only
downside I see, is that all threads have a common lock ("locks") they
need to acquire to set/unset the lock table. Although this lock is
held only for short times, I guess that if I increase the number of
threads and the data array size, it may become a contention point. I'm
thinking about arrays 2000x2000 and maybe 8 threads working on it (ie.
quad core + hyperthreading).
Phil
Don't guess, measure. It *may* be a contention point, but until you
determine that it is, don't try to work around it. If your Operation is
"a little slow", it is likely that the cost of locking is minimal.