Avatar of Rohit Bajaj
Rohit Bajaj
Flag for India asked on

Using Factory method to prevent this from escaping during construction

Hi,
Here is an example from Java concurrency in practice :

Screen-Shot-2019-03-13-at-6.26.39-PM.png
public class ThisEscape {
    public ThisEscape(EventSource source) {
} }
source.registerListener(
    new EventListener() {
        public void onEvent(Event e) {
            doSomething(e);
} });

Open in new window

As per my understanding
The above code has the following problem :
1) Since we are making an anonymous class it will contain the this object with it.
2) This is done inside a constructor before the constructor completes the execution and so the object may not be in a proper state.
So there could be a case due to very lucky timing that this was extracted out of Evenlistener and its a corrupted this.
Although once the constructor completes i think this should automatically be pointing to the complete object.

Now the book gives another example to avoid this escape of this


public class SafeListener {
    private final EventListener listener;
    private SafeListener() {
        listener = new EventListener() {
            public void onEvent(Event e) {
                doSomething(e);
} };
}
    public static SafeListener newInstance(EventSource source) {
        SafeListener safe = new SafeListener();
        source.registerListener(safe.listener);
        return safe;
} }

Open in new window


I dont quite understand how this is not escaped here.
Because here again we are using anonymous class to create an object in the same way as earlier.
So here also this should have escaped.
Please clarify.

Thanks
Java

Avatar of undefined
Last Comment
girionis

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
girionis

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Rohit Bajaj

ASKER
1) Yes but this object escapes in the second case also.
2) And in first case even though this object escapes to the listener when in a non completed state.
Still its the object pointer and when the object will be created it will be pointing to the correct one only.

I am thinking that the point of this example was to avoid escaping of this.
But this is getting escaped in both the cases anyway
girionis

In the second case it does not escape. The listener is not registered/started until *after* the object is created.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23