Java Concurrent Programming Learning notes------visibility of objects and release escapes

Source: Internet
Author: User

One, non-atomic 64-bit operation:

When a thread reads a variable without synchronization, it may get an invalid value, but at least the value is a value set by a previous thread rather than a random value, which is called the minimum security. The minimum security applies to most variables, with one exception: 64-bit numeric variables (double,long) of non-volatile types, Java memory model requirements, read and write operations for variables must be atomic operations, but for non-volatile type long, Double variable, the JVM allows a 64-bit read operation or write to be decomposed into two 32-bit operations, and when reading a long variable of a non-volatile type, if the read and write operations on the variable are performed in a different thread, Then it is possible to read the high 32 bits of a value and the lower 32 bits of the other value.

Therefore, even if you do not consider the problem of failure data, it is not safe to use variables of a type such as shared and mutable long,double in multi-threading, unless you declare them with keyword volatile or lock them up.

Second, volatile variables

When a variable is declared as a volatile type, the compiler and the runtime will notice that the variable is shared, so the operation on that variable is not reordered with other memory operations. Volatile variables are not cached in registers or in places that are not visible to other processors, so the most recent write value is always returned when reading a variable of type volatile.
The volatile limitation is that it is usually used to mark an operation complete, break, or state, and its semantics are not sufficient to ensure the atomicity of the increment operation, unless you can ensure that only one thread writes to the variable.
You should use a volatile variable only if and only if the following conditions are true:
1) The write operation to the variable does not depend on the current value of the variable, or ensures that only a single thread updates the value of the variable;
2) The variable is not included in the invariant condition with other state variables;
3) No lock is required when accessing variables.

The locking mechanism ensures both visibility and atomicity, while volatile variables only ensure visibility.

Third, the release of the object to escape

The following example code:

public class Thisescape {public    thisescape (EventSource source) {        source.registerlistener{            new EventListener () () {public                void OnEvent (Event e) {                    dosomething (e);                }        }}    }

This class is not secure, because when Thisescape publishes EventListener, it also implicitly publishes the Thisescape instance itself, because an instance of this inner class contains an implicit application of the Thisescape instance, so When you publish an object from the object's constructor, only one object that has not yet been constructed is published. This is true even if the statement of the publication object is in the last row of the constructor. There is no error in creating a thread in the constructor, but it is best not to start it immediately, but to start with a start or Initialize method , for example, to prevent the this reference from escaping during construction by using the following factory method:

public class Safelistener {    private static final EventListener listener;    Public Safelistener () {        listener = new EventListener () {public            void OnEvent (Event e) {                dosomething (e);            }        }    }    public static Safelistener newinstance (EventSource source) {        Safelistener safe = new Safelistener ();        Source.registerlistener (Safe.listener);        return safe;    }}

  

Java Concurrent Programming Learning notes------visibility of objects and release escapes

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.