Chapter Two: Thread safety
2.1 What is thread safety
Can be called by more than one thread, and no error interaction occurs between threads
A local variable within a method does not need to be protected because it is stored in the stack and is unique to each thread
2.2 atomicity
A shared variable can be defined as an atomic variable: atomic
Multiple shared variables, there may be some kind of dependency between, defined as an atomic variable will be due to race conditions, errors, such as the first check after execution.
Race condition: The correctness of a calculation depends on the alternating execution timing of multiple threads
This requires a composite operation, the read and write operations of multiple variables as a block, followed by the
2.3 Plus lock mechanism
A composite block is locked with a built-in lock. Synchronized, the method called the object as a lock, the equivalent of a mutex, the program block can only be entered by the object, but because the built-in lock is reentrant, that is, the program block can be accessed multiple times by the object, often used in sub-class replication base class synchronized method.
2.4 Using locks to protect status
There are not only built-in locks available, but you can also perform synchronization with the displayed locks, and additional locking mechanisms are required when multiple synchronized methods are interdependent.
2.5 Activity and performance
Because the above method can cause very low performance, if a program block takes a long time, then the subsequent thread will wait for longer, should say the lock granularity is reduced, but in the performance and program simplicity to strike a balance
Chapter III Sharing of objects
3.1 Visibility
If thread a modifies the variable I, can thread B see it immediately?
Synchronized can be used not only to lock, but also to synchronize
Non-atomic 64-bit operations: will be decomposed into two 32-bit operations, so it is necessary to declare a shared and variable long or double type of data in a multithreaded environment, with volatile modifiers, or lock protection. Volatile is used to synchronize updates of variables to other threads, only for visibility, not for atomicity. The lock operation is not performed when accessing volatile variables, so volatile is a more lightweight synchronization mechanism than synchronized.
The choice of volatile and locking:
Volatile is commonly used in simple state flags, but does not guarantee incrementing atomicity, in 1, the write to the variable does not depend on the current value of 2, the variable is not included in the immutable condition with other variables 3, is used when the variable is accessed without locking.
Lock to ensure atomicity, but also to ensure visibility.
3.2 Release and escape of objects
When an object is published, the non-private objects referenced within the object are published together.
The this pointer in the constructor is easily escaped and can be avoided by using a private constructor + instance factory
3.3 Thread closure
Data is accessed only in single-wire range, called thread closure. such as JDBC's Connection object. Gets a connection object from the connection pool until it is exhausted, and the connection pool does not assign the object to other threads.
Stack closure: declared as a local variable in a method
Threadlocal class: Used to prevent the sharing of mutable single-instance variables or global variables. For example, a connection object that is used in a process can be used by all methods within it, but cannot be accessed by other processes.
3.4 invariance
Immutable variable: Final
3.5 Security release
Shared objects between multiple threads, you must ensure secure sharing
Mutable objects must be published in a secure manner, that is, synchronization must be used. A properly constructed object can be published in the following ways: 1. Initialize an object reference 2 in a static initialization function, save the object's reference to a volatile type of field, or an object of type Atomicreferance 3, Save the object's reference to the final field of a properly constructed object 4, save the object's reference to a domain protected by a lock
The basic knowledge and object sharing of Java Concurrent Programming practical learning Notes