We help IT Professionals succeed at work.

Using Factory method to prevent this from escaping during construction

Rohit Bajaj
Rohit Bajaj asked
on
96 Views
Last Modified: 2019-04-23
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
Comment
Watch Question

CERTIFIED EXPERT
Distinguished Expert 2019
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

Author

Commented:
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
CERTIFIED EXPERT
Distinguished Expert 2019

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

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions