Home >Java >javaTutorial >How to publish and avoid object escape problems in Java?
Simply put, it is to provide a reference to an object to code outside the scope. For example, return an object, or pass it as a parameter to a method of other classes.
Unsafe publishing object example:
<code>@Slf4j</code><code>@NotThreadSafe</code><code>public class UnsafePublish {</code><code><br></code><code> private String[] states = {"a", "b", "c"};</code><code><br></code><code> public String[] getStates() {</code><code> return states;</code><code> }</code><code><br></code><code> public static void main(String[] args) {</code><code> UnsafePublish unsafePublish = new UnsafePublish();</code><code> log.info("{}", Arrays.toString(unsafePublish.getStates()));</code><code> // 发布对象不安全,可被修改</code><code> unsafePublish.getStates()[0] = "d";</code><code> log.info("{}", Arrays.toString(unsafePublish.getStates()));</code><code> }</code><code>}</code>
If a class has provided an object reference to the external code and released the object before the construction is completed, it is called an object escape. The escape of the object will Violates thread safety.
<code>public class Escape {</code><code> private int thisCanBeEscape = 1;</code><code><br></code><code> public Escape() {</code><code> new InnerClass();</code><code> // 还有业务需要执行</code><code> thisCanBeEscape++;</code><code> }</code><code><br></code><code> private class InnerClass {</code><code> public InnerClass() {</code><code> log.info("{}", Escape.this.thisCanBeEscape);</code><code> }</code><code> }</code><code><br></code><code> public static void main(String[] args) {</code><code> new Escape();</code><code> }</code><code>}</code>
The instance of this inner class contains a reference to the private domain object of the encapsulated instance. It will be released before the object is correctly constructed, which may be unsafe. Inside, there is an error that causes this reference to overflow during construction.
The above code starts a thread during function construction. Whether it is implicit startup or explicit startup, it will cause the overflow of this reference. The new thread will always see the owning object before it is constructed.
The syntax of "class name.this" is called "qualified this" in the Java language. The main purpose of this syntax is: in the method of an inner class, when you want to specify the "this" reference of an outer class at a certain nested level, use the "outer class name.this" syntax. For example:
class Foo { class Bar { Foo getFoo() { return Foo.this; } }}
In the getFoo() method in the Foo.Bar class, if you write "this" directly, it refers to the instance of the Foo.Bar class, and if you want to specify the this instance of the peripheral Foo class , it must be written as Foo.this here. In particular, if you write Bar.this in the getFoo() method in the above example, the effect is the same as writing this directly, specifying the current Foo.Bar class instance.
Initialize an object reference in the static initialization function
Save the object reference to the volatile type field or AtomicReference object
Save the reference of the object to a final type field of a correctly constructed object
Save the reference of the object To a domain protected by a lock
You can think of the hungry mode/lazy mode in the singleton mode.
Thread restriction: An object restricted by a thread is exclusive to the thread and can only be modified by the thread that owns it.
Shared read-only: A shared read-only object can be accessed concurrently by multiple threads without additional synchronization, but no thread can modify it.
Thread-safe object: A thread-safe object or container uses a synchronization mechanism to ensure thread safety internally, so other threads can access it at will through the public interface without additional synchronization.
Guarded object: The guarded object can only be accessed by acquiring a specific lock.
The above is the detailed content of How to publish and avoid object escape problems in Java?. For more information, please follow other related articles on the PHP Chinese website!