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 ?