This escape means that other threads hold references to the object before the constructor returns. Calling methods that have not yet constructed an object may cause confusing errors, so this escape should be avoided.
This escape often occurs when starting a thread or registering a listener in a constructor, such as:
public class ThisEscape { public ThisEscape() { new Thread(new EscapeRunnable()).start(); // ... } private class EscapeRunnable implements Runnable { @Override public void run() { // Through ThisEscape.this, peripheral class objects can be referenced, but at this time, the peripheral class objects may not have been constructed yet, that is, the escape of this reference of the peripheral class occurred} } }Solution
public class ThisEscape { private Thread t; public ThisEscape() { t = new Thread(new EscapeRunnable()); // ... } public void init() { t.start(); } private class EscapeRunnable implements Runnable { @Override public void run() { // Through ThisEscape.this, you can refer to peripheral class objects. At this time, you can ensure that the peripheral class objects have been constructed} } }The above is all the content compiled by the editor this time. Thank you for your support to Wulin.com.