I was recently going through "Java Concurrency in practice" and came across example of Publication and Escape , after going through it several times I still feel that I didn't completely understood the example.
public class ThisEscape{
public ThisEscape(EventSource source){
source.registerListener(
new EventListener(){
public void onEvent(Event e){
doSomething(e);
}
}
)
}
}
ThisEscape illustrates an important special case of Escape - when the this references escapes during construction. When the inner EvenListener instance is publised ,so is the enclosing ThisEsape instance.But and object is in a predictable ,consistent state only after its constructuor return. so publishing an object from within its constructor can publish an incompletely constructed object.This is true even if the publication is the last statement in the constructor.If the this reference escapes during construction, the object is considered not properly constructed.
ThisEscape cosntructor is registering a EventSource with and EventListener where we are specifying an onEvent behaviour passing an Event instance.
But here I assume the order of object construction to be EventListener --> ThisEscape so how the this reference of ThisEscape is being passed here to escape ?

I believe we should refer to this excerpt:
Which makes me think
doSomething()is a method ofThisEscape(otherwise the reference would not escape), meaningThisEscapeis a listener forEventSource, and it registers itself during construction via overridingEventListener'sonEventmethod.In simple words, it says: "when a new event occurs, call
doSomething()".At this point the problem is quite simple. Imagine it, we've registered a new listener inside
ThisEscape's constructor... and immediately... an event occurs! While one thread is still constructingThisEscape, another one is already callingdoSomething(), which may lead to a very unpredictable result...