Object publishing and escaping in Java thread security

Source: Internet
Author: User

The two concepts of release (Publish) and Escape (escape) are the first to be heard, but it is very common in practice, which is very much related to the thread safety of Java concurrent programming.

What is a release? In simple terms, provide a reference to an object outside the scope of the code. For example, return an object, or pass it as a parameter to a method in another class.

What is escaping? If a class has not yet been constructed, it has been provided to external code an object reference is published, when the object is escaped, and the object's escape destroys the thread's security.

We know the concept, but where do we focus? We need to focus on the problem, do not publish the object in the place do not publish the object, such as the following code:

1 class unsafestates{2     Private New string[]{"AK", "AL"}; 3 4      Public string[] GetStates () {5         return states; 6     }7 }

The scope of the states variable is private and we publish it in the GetStates method, which is called an array states that escapes its scope.

However, it is more covert and needs our attention that this escape, this problem should be the focus of attention. What is this escape? Observe the following code:

1  Public classthisescape{2     Private intvalue;3      PublicThisescape (EventSource source) {4 source.registerlistener{5             NewEventListener () {6                  Public voidonEvent (Event e) {7 dosomething (e);8                 }9             }Ten         } One         //Some of the initialization work AValue = 7;  -     } -  the      Public voiddosomething (Event e) { - System.out.println (value); -     } -  +}

In the construction method we define an anonymous inner class, an anonymous inner class is an event listener class, and when the event listener class is registered, we have actually published the EventListener anonymous inner class, and we have actually carried this escape, The point is that at this point we still have some initialization work not done (after the code 11 lines), which is what we said above, a class has not been constructed and we are already releasing it. How to avoid this escape? Now that we have not constructed the constructor, we will construct the constructor so that the constructor is defined as the private scope. As shown in the following code:

1  Public classsafelistener{2     Private FinalEventListener Listener;3 4     PrivateSafelistener () {5Listener =NewEventListener () {6              Public voidonEvent (Event e) {7 dosomething (e);8             }9         }Ten     } One  A      Public StaticSafelistener newinstance (EventSource source) { -Safelistener Safelistener =NewSafelistener (); - Safelistener.registerlistener (safelistener.listener); the  -         returnSafelistener; -     } -}

We first set the constructor to private, and then we do not publish the object when the constructor is complete, but instead use the factory method, The object is published after the constructor has been executed in the factory method Newinstance (the code is registenerlistener registered for monitoring). This is actually a modification of the serial execution mode for the publishing object, not the previous asynchronous pattern, so that it does not pose a thread-safety issue to us.

Object publishing and escaping in Java thread security

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.