Release escape Java this escape "go"

Source: Internet
Author: User

Transferred from: http://blog.csdn.net/joker_zhou/article/details/7322801

(1) Publish: A publication refers to an object that stores its references to a place where other code can be accessed, returns the reference in a non-private method, or passes it to other objects.

A) The simplest thing to publish is to set the object to a public static domain

b) The second simple way to do this is to return a reference to the object directly within a public method

The third kind of release is very secretive. It's about itself. The object reference is published in a reference to another object.
May appear in the construct so this is this example:

In this way, source holds Thisescape's inner class Evenlistener () and Evenlistener may bring out the protected data references in Thisescape, and can use factory and private constructs to prevent release from escaping. This allows the Thisescape object to be constructed and then put into the listener using Thisescape.registerlistener (source). It is best not to register listeners or start threads within a construct. And initialize these things inside the factory.

classa1{ PublicEventListener l;}  Public classmythread{EventListener L=NULL; PrivateMyThread () {L=NewEventListener () {}; }     Public StaticMyThread newinstance (A1 a) {MyThread my=NewMyThread (); <span style= "White-space:pre" > </span> a.l=MY.L; returnmy; }}

In fact A1 in the L I have no encapsulation. Should be encapsulated. I'm just covering up. How to register

(2) Escaping: If an object publishes it before it is finished, it is escaping. Starting a thread in a construct causes the object to be released without constructing the completed objects, resulting in an escape.

(3) How to secure the release:

A) Typically the release thread needs to be synchronized with the consuming thread.

b) Initializes a reference to the initial object by statically initializing it.

c) Reference storage to volatile or atomicreference

d) Reference is stored in the final domain where the object is created correctly

(4) If you need to publish a non-thread-safe set of States (that is, objects that contain basic types), you can first copy the objects and then publish the copied objects. This avoids foreign clients from modifying non-thread-safe classes. This assumes that the collection state is not required to be consistent across multiple threads.

(5) Publish a thread-safe class that does not require replication-publishing.

(6) You can safely publish a thread-safe class and publish without any of my constraint behavior

Release escape Java this escape "go"

Related Article

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.