Avatar of Rohit Bajaj
Rohit BajajFlag for India

asked on 

Using Factory method to prevent this from escaping during construction

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

User generated image
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
ASKER CERTIFIED SOLUTION
Avatar of girionis
girionis
Flag of Greece image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Rohit Bajaj
Rohit Bajaj
Flag of India image

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
Avatar of girionis
girionis
Flag of Greece image

In the second case it does not escape. The listener is not registered/started until *after* the object is created.
Java
Java

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.

102K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo