1, non-denaturing
When an object is created and its state cannot be modified, the object is called an immutable object, and the immutable object must be thread-safe. immutable objects are simple. They have only one state, and the state is controlled by the constructor.
The object is immutable when the following conditions are met: (1), the state cannot be changed after the object is created, (2), all the fields of the object are final types, and (3) the object is created correctly (this reference does not overflow during object creation).
1.1 Final Domain
The keyword final is used to construct immutable objects, and final types of fields cannot be modified (but the objects referenced by the final field are mutable, and those referenced objects can be modified), even if the object is mutable by declaring some fields of the Mutable object as final types. The equivalent of telling the maintainer that these domains are immutable.
2. Publish an object correctly
Two problems encountered when an object is published correctly:(1) The reference itself is to be seen by other threads, and (2) the state of the object to be seen by other threads.
In multithreaded programming, the first principle is to avoid the sharing of objects, because if there is no object sharing, then multi-threading is much easier to write, but if you want to share the object, in addition to correctly write the constructor correctly, how to correctly publish is also a very important issue.
Let's look at the following code:
1 Public classClient {2 PublicHolder Holder;3 4 Public voidInitialize () {5Holder =NewHolder (42);6 }7 }8 9 Ten Public classHolder { One intN; A PublicHolder (intN) { - This. N =N; - } the Public voidassertsanity () { - if(n! =N) - Throw NewAssertionerror ("This statement is false.")); - } +}View Code
In the client class, the holder object is published, but this is an incorrect publication. Because of visibility issues, the holder object that other threads see will be in an inconsistent state, even if the invariant condition has been built correctly in the constituent constructor of the object, and this incorrect publication causes other threads to see objects that have not yet been created. The main is that the creation of the holder object is not atomic and may not have been constructed yet, and other threads are starting to invoke the holder object.
Because the synchronous method is not used to ensure that the holder object (which contains both references and object state) is visible to other threads, the holder becomes improperly published . The problem is not the holder itself, but its lack of proper publishing. There are no issues that could be caused by the correct publication:
- Other threads may see obsolete values for the holder field, which results in a null reference or an obsolete value (even if holder is already set) (the reference itself is not seen by another thread)
- Even worse, for the updated holder, you can see the referenced updates in time, but for the state of the object, you can see the old value, for the above code, may throw Assertionerror exception
The main is Holder = new Holder (42); This code is not atomic, and other threads may invoke the Holder object reference when the construct is incomplete, leading to unpredictable results.
2.1 Common patterns for secure publishing
To safely publish an object, the object's reference and the state of the object must be visible to other threads at the same time. Normally a properly constructed object (the constructor does not take place this escape) can be published correctly in the following way:
(1), initializing an object reference in a static initialization function
(2), save an object reference in a volatile type of field or in a Atomicreference object
(3), save the object's reference to a domain of the final type of a properly constructed object.
(4), save the object's reference to a lock-protected domain.
The internal synchronization of the thread security container means that the last requirement above will be met by placing the object in a container, such as a vector. If thread a puts object x in a thread-safe container and then thread B reads the object, you can ensure that B sees the X state of a set, even if the application code for this read/write x does not contain the displayed synchronization. Security releases are guaranteed in the following containers:
(1), by placing a key or value in Hashtable, Synchronizedmap, or concurrentmap, you can safely publish it to any thread that accesses it from those containers.
(2), by placing an element into the vector, copyonwritearraylist, Copyonwritearrayset, synchroizedlist, you can safely publish the element to any thread that accesses the element from those containers.
(3), by placing the element in Blockingqueue or concrrentlinkedqueue, you can safely publish the element to any thread that accesses the element from those access queues.
In general, the simplest and safest way to publish a statically constructed object is to use a static initializer: public Static Holder = new Holder;
Static initializers are performed by the JVM during the initialization phase of the class, and because of the synchronization mechanism inside the JVM, the initialization of objects in this manner can be safely published. For mutable objects, it is safe to publish at the time to ensure visibility of the 发布当时 state, and in subsequent accesses to each object, synchronization is also required to ensure the visibility of the modification operation.
Java Multithreading--invariance and security publishing