Home > Java > javaTutorial > How to publish and avoid object escape problems in Java?

How to publish and avoid object escape problems in Java?

WBOY
Release: 2023-04-22 14:37:08
forward
988 people have browsed it

Publish Object

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>
Copy after login

Object escape

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>
Copy after login
  • 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.

Explanation of class name.this

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;    }  }}
Copy after login

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.

Safely publish objects

  1. Initialize an object reference in the static initialization function

  2. Save the object reference to the volatile type field or AtomicReference object

  3. Save the reference of the object to a final type field of a correctly constructed object

  4. 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.

Safe shared object policy

  1. Thread restriction: An object restricted by a thread is exclusive to the thread and can only be modified by the thread that owns it.

  2. Shared read-only: A shared read-only object can be accessed concurrently by multiple threads without additional synchronization, but no thread can modify it.

  3. 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.

  4. 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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template