Combination of objects "Java Concurrency combat" the

Foreword

This series is a little summary of blog reading "Java Concurrency in combat," the feeling of reading this book is too dull, and so I plan to read summary under review, who want to quickly understand the content of this book friends, this is very suitable for reading series blog.

Design thread-safe class

This section briefly describes how to design a thread-safe class, in the process of designing a thread-safe class, you need to include the following three elements:

  • Identify all variables including the state of the object.
  • Find out the condition of invariance constraints state variables.
  • Concurrent access management policy established state of the object.
  1. To create a thread-safe class, we must first understand what the state is requiring synchronization, it is to first collect synchronization requirements. For example: when a class variable int value is 10, plus one each time, then the next time is only 11. When the next state of the need to rely on the current state, this operation must be a complex operation. This must be an atomic operation.
  2. Invariance condition posteriori constraint condition based on a subject which has the states and state transitions are effective in the method also contains some objects based on the state of some a priori conditions, such as: can not be removed from an empty queue an element before removing elements, the queue must be non-empty state. If the operation based on the state of a class priori included, then this operation is called a dependent operating state, may be realized by a state-dependent (e.g. Blocking Queue or queue blocking semaphore) existing class library behavior .
  3. There are ownership model in Java, the garbage collector just reduced our many common error in reference to sharing, thus reducing the cost of ownership in the process, ownership and packaging are always interrelated, object encapsulates its own state, whereas the state ownership of the package, all state variables that will determine what kind of locking protocol employed to maintain the integrity of the variables of state ownership means control. This is an element of a re-design class need to be considered.

对象的状态:对象的状态由变量构成,就是类中的字段。

不变性条件:不变性条件是,在程序执行过程或部分过程中,可始终被假定成立的条件。比如,循环的不变性条件就是只有为真才开始循环。

Examples closed

In the previous section we said closed thread, the thread closed mainly used to encapsulate data into the internal thread, only for its own use. Then the instance is closed doing? If an object is not thread safe, you can make it safe to use in multithreaded programs through a variety of techniques. In this section introduces a Java monitor mode, all states follow the Java variable monitor mode will target objects are encapsulated by the object's own built-in lock to protect.

//伪代码
public final class Counter{
    private long value = 0;
    
    public synchronized long getValue(){
        return value;
    }
    public synchronized long increment(){
        return ++value;
    }
}
复制代码

Many Java, Java classes are used, for example, monitor mode ,, and Vector Hashtable, Java mode only monitor the code written in a convention.

Thread safety commission

  1. When the security thread class entrusted to a plurality of state variables, as long as these variables independent of each other, i.e., the combination did not increase in any invariant conditions comprising a plurality of variable state. If each of the variables are thread-safe, then this class is thread-safe, it can be their own thread safety entrusted to its own variables, provided that these variables are independent of each other with each other directly, and is thread safe.
  2. Most combinations of objects which exist between certain invariance condition of their state variables, when there will be between state variables associated with complex operation, then only rely on commission and not enough to achieve thread safety, must in this case provide your own locking mechanism to ensure that complex operations are atomic.
  3. If a state variable is thread-safe, and there is no invariance conditions to constrain its value, operations on variables nor is there any state does not allow conversion, it can be safely released this variables.

Existing thread-safe classes to add functionality

Sometimes, an existing thread-safe classes can support all the operations that we need, but more often, existing class can only support most of the operations, it is necessary at this time without damaging the thread safety adding a new operation.

  1. Client client locking mechanism locking means, for use as a pair of X client code, using lock X itself to protect its state to protect this client code to use client-side locking, you must You know which object X using a lock. The client lock is vulnerable because it is locked class code is distributed to the plurality of classes. It is to use the party must be locked.
  2. combination
//伪代码
public class ImprovedList<T> implements List {
    private final List<T> list;

    public ImprovedList(List<T> list) {
        this.list = list;
    }

    public synchronized boolean putIfAbsent(T x) {
        boolean contains = list.contains(x);
        if (!contains) {
            list.add(x);
        }
    }

    //按照此种方式委托list的其他方法
    public synchronized void clear() {
        list.clear();
    }
}
复制代码

Examples List ImprovedList code to the underlying operation is implemented by the operation of the List List object delegate, if not then a new method is added. That's portfolio. In fact the use of Java monitor pattern to encapsulate existing List, and as long as a single point to the underlying List of external references in the class, will be able to ensure thread safety.

After a hard point you can see it like a focus point oh! View profile, there are more blog Oh. If wrong, please correct me. AC plus interest group together.

Guess you like

Origin juejin.im/post/5d5aa5d3e51d456209238857