I went through the Oracle tutorial on concurrency and threading and came across the following lines:
Warning: When constructing an object that will be shared between threads, be very careful that a reference to the object does not “leak” prematurely. For example, suppose you want to maintain a List called instances containing every instance of class. You might be tempted to add the following line to your constructor: instances.add(this); But then other threads can use instances to access the object before construction of the object is complete.
Could any one can explain what these lines mean exactly?
imagine you have two threads. left thread and right thread. left thread is responsible for creating worker objects, and right thread is responsible for putting them to work.
when the left thread has finished creating an object, it puts the object in a location where right thread can find it. it’s a variable, let’s call it w (for worker), and for simplicity, let’s say it’s somehow globally accessible.
the right thread is a loop. it checks if w is not null. if w in fact has a none null value, then the method
dois called on it.the class worker looks like this:
so it goes something like this (i’ve tried illustrating the two threads by setting their respective commands in one column each. i hope it’s understandable. remember, only one thread is active at once, so that’s why there’s always only instructions in one column at a time)
in this scenario, all is well. left thread set w after the constructor of worker had completed. but isn’t it stupid to do it like that? imagine the savings we could do if we put that call
w = instanceOfWorkerinside the worker constructor. then we wouldn’t have to worry about remembering to actually set w.new constructor of worker looks like this:
now, the flow of the code could end up looking like this:
oracle has a more intricate example with a collection of objects called “instances”. that’s the only difference.