Volatile variable
The role of the Volatile keyword:
- Ensure that actions on declared variables are not reordered with other memory operations
- Volatile variables are not cached to registers or other places where the processor is hidden (guaranteed visibility)
- Ensure that the reference type,
long
and double
the atomic value of the Read or assignment
Main uses of Volatile:
- Ensure the visibility of the state of the objects they reference
- Ensure
long
the atomicity of reading or assignment and double
type
- Ensure visibility of flag status changes
Release and Escape
Publish: An object can be used by code outside the current scope. Like what:
- Passed as a parameter to another method
- Returns a reference to this object as a return value
Escape: An object is released when it is not ready. Like what:
- The constructor has a parameter of the collection type, but in the constructor
collection.add(this)
, this
it is escaped.
Note: Do not start a thread in the constructor. Because when an object creates a thread in the constructor, either explicitly or implicitly, the this
reference is almost always shared by the new thread. You can create a thread in the constructor, but don't start it!
Thread closure
Threads need to be synchronized because the data is shared between threads . So the way to avoid synchronization is to not share data , also called thread closure .
The JDBC object of the application pool is a thread-gated example, where each Connection object is always exclusive to a thread until it is returned to the pool.
Ad-hoc Thread Limits
Features: Error prone, should be replaced with stack limit and ThreadLocal.
Stack limit
Local variables are restricted to the execution thread, they exist in the execution line stacks , and other threads cannot position the stack.
Note: Make sure that local variables are not escaped!
ThreadLocal
ThreadLocal
Multiple objects can be bound in the execution thread, ThreadLocal
providing the get
and set
methods to maintain a separate copy of each thread that uses it.
ThreadLocal
Commonly used in:
- Prevent incorrect sharing in designs based on mutable monomers or global variables. such as
SimpleDateFormatter
andConnection
- A frequently performed operation requires a temporary object such as buffer and avoids reassigning the temporary object each time.
Threadlocal:every thread has a threadlocals map structure to store value related on the thread.
When calling Threadlocal.get ():
ThreadLocal Firstly try to get the Threadlocals map structure:
Immutable objects
The publication object depends on the variability of the object:
- Immutable objects can be published by any mechanism
- Efficient immutable objects must be published securely
- Mutable objects must be securely published and must be thread-safe or lock-protected
Immutable objects:
Immutable objects are objects whose post-construction state cannot be changed. Because of its immutability, he will not have Thread interference
and Memory inconsistent
so on.
Characteristics of Immutable objects:
- Do not set the
setter
method.
- All members are set to
final
+private
- Subclasses are not allowed to override methods. The simple approach is to put the class declaration before the +
final
If there are reference types in the instance variables, do not let them be changed:
Immutable objects are always thread-safe. It has only one state and cannot be changed. Immutable objects have the following characteristics:
- is created correctly (no
this
reference escapes during creation)
- All fields are
final
types
- The status cannot be modified after it is created
Note: If the final
domain points to mutable objects, you still need to synchronize when accessing the state of those objects.
How can I guarantee the visibility of an immutable object ?
- Static initializer Initializes a reference to the object
- Store its references to a
volatile
domain orAtomicReference
- Store its reference in the domain of the object you created correctly
final
- Its reference is stored in a domain that is properly protected by the lock
Valid immutable objects
An object is not technically immutable, but its state is not modified after it is published, such an object is called a valid immutable object.
Any thread can safely use a highly efficient and immutable object that is securely released without additional synchronization.
mutable objects
Security releases only guarantee visibility of the "release at the time" state. But then the change of state requires thread synchronization to ensure.
Java Concurrent Programming-1