Assuming that I have the following code:
final Catalog catalog = createCatalog();
for (int i = 0; i< 100; i++{
new Thread(new CatalogWorker(catalog)).start();
}
“Catalog” is an object structure, and the method createCatalog() and the “Catalog” object structure has not been written with concurrency in mind. There are several non-final, non-volatile references within the product catalog, there may even be mutable state (but that’s going to have to be handled)
The way I understand the memory model, this code is not thread-safe. Is there any simple way to make it safe ? (The generalized version of this problem is really about single-threaded construction of shared structures that are created before the threads explode into action)
No, there’s no simple way to make it safe. Concurrent use of mutable data types is always tricky. In some situations, making each operation on
Catalogsynchronized (preferably on a privately-held lock) may work, but usually you’ll find that a thread actually wants to perform multiple operations without risking any other threads messing around with things.Just synchronizing every access to variables should be enough to make the Java memory model problem less relevant – you would always see the most recent values, for example – but the bigger problem itself is still significant.
Any immutable state in
Catalogshould be fine already: there’s a “happens-before” between the construction of theCatalogand the new thread being started. From section 17.4.5 of the spec:(And the construction finishing happens before the call to
start(), so the construction happens before any actions in the started thread.)