Background: I have got a (more or less) huge data model in the memory. The model contains around 3.150.000 to 12.600.000 objects that could be modified directly. In addition, there are around 75.000.000 objects that can only be modified via those 3.150.000 to 12.600.000 objects.
On the other hand, there are around 10 modules which accessing the model. These modules can be grouped into:
- reading and modifying some of the objects every 250 ms to 1000 ms
- reading and modifying some of the objects on demand
- reading the some of the objects if they have been changed
Question: How to synchronize such a data model? There are the following ideas in my mind:
(1) A lock in every class that can be directly modified. Advantage: Only the objects that are modified must be locked. Disadvantage: A high synchronization effort and a huge amount of lock instances (3.150.000 to 12.600.000 additional objects/locks). There is a great danger of doing something wrong in the synchronization (deadlocks, etc.).
(2) A central interface to access the whole data model. This interface would lock the whole model on every modification via a single lock. Advantage: Only a single lock --> less synchronization effort. Disadvantage: The whole model is locked regardless of the type of change.
(3) Dispatch Thread (like in AWT/Swing). A thread which processes tasks (events). Advantage / disadvantage like idea (2). However, this would be a event based solutuion. I read Graham Hamilton's article about multi-threading in GUI-tollkits. In addition, there is a great talk about "Events versus Threads" by John Ousterhout. Of course my data model isn't that extensive, but the article gets to the heart of the matter.
Here the link to Graham Hamilton's article: https://weblogs.java.net/blog/kgh/archive/2004/10/multithreaded_t.html
So, what are your experiences? Maybe you have a better idea.
EDIT: I made a big mistake on the object calculation. I just updated the amount.
Thanks in advance :)
EDIT 2: Here a model I just created for demonstration purposes:
enum Ware { WOOD, COAL, STONE }
class Stock { Map<Ware, Integer> internalStock; }
class Coordinate { int x; int y; }
interface ILand {}
class World {
Map<Coordinate, ILand> land;
Map<Coordinate, Ship> ships;
}
class Island implements ILand { Stock resources; }
class Ship { Stock stock; }
class Building {Stock stock; }
class Colony implements ILand {
Island builtOn;
Set<Building> building;
}
class Character {
Set<Colony> colonies;
Set<Ship> fleet;
}
This would be the strucure of the data model:
Model
World <>--- ILand
<>--- Ship
Character <>--- Colony <>--- Building <>--- Stock
<>--- Island <>---Stock
<>--- Ship <>--- Stock