This was asked slightly differently earlier but asking for a yes/no answer but I'm looking for the explanation that's missing from the book (Java Concurrency in Practice), of how this apparent big mistake would be exploited maliciously or accidentally.
A final mechanism by which an object or its internal state can be published is to publish an inner class instance, as shown in ThisEscape in Listing 3.7. When ThisEscape publishes the EventListener, it implicitly publishes the enclosing ThisEscape instance as well, because inner class instances contain a hidden reference to the enclosing instance.
Listing 3.7. Implicitly Allowing the this Reference to Escape. Don't do this.
public class ThisEscape { public ThisEscape(EventSource source) { source.registerListener( new EventListener() { public void onEvent(Event e) { doSomething(e); } }); } }
3.2.1. Safe Construction Practices
ThisEscape illustrates an important special case of escape—when the this references escapes during construction. When the inner EventListener instance is published, so is the enclosing ThisEscape instance. But an object is in a predictable, consistent state only after its constructor returns, 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.[8]
[8] More specifically, the this reference should not escape from the thread until after the constructor returns. The this reference can be stored somewhere by the constructor so long as it is not used by another thread until after construction. SafeListener in Listing 3.8 uses this technique.
Do not allow the this reference to escape during construction.
How would someone code against this to get to the OuterClass before it's finished constructing? What is the hidden inner class reference
mentioned in italics in the first paragraph?
If you want your inner class to access outer class instance variables then in the constructor for the inner class, include an argument that is a reference to the outer class instance. The outer class invokes the inner class constructor passing this as that argument.
Of your inner class is non static then create an object of innerClass. OuterClass out = new OuterClass(); OuterClass. InnerClass inn = out.
Non-static nested classes (inner classes) have access to other members of the outer/enclosing class, even if they are declared private.
Inner class can extend it's outer class. But, it does not serve any meaning. Because, even the private members of outer class are available inside the inner class. Even though, When an inner class extends its outer class, only fields and methods are inherited but not inner class itself.
Please see this article. There it's clearly explained what could happen when you let this
escape.
And here is a follow-up with further explanations.
It's Heinz Kabutz amazing newsletter, where this and other very interesting topics are discussed. I highly recommend it.
Here is the sample taken from the links, which show how the this
reference escapes:
public class ThisEscape { private final int num; public ThisEscape(EventSource source) { source.registerListener( new EventListener() { public void onEvent(Event e) { doSomething(e); } }); num = 42; } private void doSomething(Event e) { if (num != 42) { System.out.println("Race condition detected at " + new Date()); } } }
When it gets compiled, javac generates two classes. The outer class looks like this:
public class ThisEscape { private final int num; public ThisEscape(EventSource source) { source.registerListener(new ThisEscape$1(this)); num = 42; } private void doSomething(Event e) { if (num != 42) System.out.println( "Race condition detected at " + new Date()); } static void access$000(ThisEscape _this, Event event) { _this.doSomething(event); } }
Next we have the anonymous inner class:
class ThisEscape$1 implements EventListener { final ThisEscape this$0; ThisEscape$1(ThisEscape thisescape) { this$0 = thisescape; super(); } public void onEvent(Event e) { ThisEscape.access$000(this$0, e); } }
Here the anonymous inner class created in the constructor of the outer class is converted to a package-access class that receives a reference to the outer class (the one that is allowing this
to escape). For the inner class to have access to the attributes and methods of the outer class, a static package-access method is created in the outer class. This is access$000
.
Those two articles show both how the actual escaping occurs and what might happen.
The 'what' is basically a race condition that could lead to a NullPointerException
or any other exception when attempting to use the object while not yet fully initialized. In the example, if a thread is quick enough, it could happen that it runs the doSomething()
method while num
has not yet been correctly initialized to 42
. In the first link there's a test that shows exactly that.
EDIT: A few lines regarding how to code against this issue/feature were missing. I can only think about sticking to a (maybe incomplete) set of rules/principles to avoid this problem and others alike:
private
methods from within the constructorprotected
methods from within the constructor, do it, but declare these methods as final
, so that they cannot be overriden by subclassesthis
directly as an argument to anything private
or protected final
method that is invoked from within the constructorIf you need to do further things, use either the builder or the factory pattern.
I'll modify the example a bit, to make it more clear. Consider this class:
public class ThisEscape { Object someThing; public ThisEscape(EventSource source) { source.registerListener( new EventListener() { public void onEvent(Event e) { doSomething(e, someThing); } }); someThing = initTheThing(); } }
Behind the scenes, the anonymous inner class has access to the outer instance. You can tell this, because you can access the instance variable someThing
and, as Shashank mentioned you can access the outer instance via ThisEscape.this
.
The problem is that by giving the anonymous inner class instance to the outside (in this case the EventSource
object) it will also carry the ThisEscape instance with it.
What can happen bad with it? Consider this implementation of EventSource below:
public class SomeEventSource implements EventSource { EventListener listener; public void registerListener(EventListener listener) { this.listener = listener; } public void processEvent(Event e) { listener.onEvent(e); } }
In ThisEscape
's constructor we register an EventListener
which will be stored in the listener
instance variable.
Now consider two threads. One is calling the ThisEscape
constructor, while the other calls processEvent
with some event. Also, let's say the JVM decides to switch from the first thread to the second one, right after the source.registerListener
line and right before someThing = initTheThing()
. The second thread now runs, and it will call the onEvent method, which as you can see, does something with someThing
. But what is someThing
? It's null, because the other thread did not finish initializing the object, so this will (probably) cause a NullPointerException, which is not actually what you want.
To sum up: be careful that you don't escape objects that have not been fully initialized (or in other words, their constructor did not finish yet). One subtle way you could inadvertently do this, is by escaping anonymous inner classes from the constructor, which will implicitly escape the outer instance, which is not fully initialized.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With