Reading notes | Java thread safe and lock optimization

Disclaimer: This article is a blogger original article, please indicate the source: https://blog.csdn.net/qq_38182125/article/details/90812394

I. Overview

This article is based on "in-depth understanding of the Java Virtual Machine," the study notes a book, made a presentation for the thread-safe as well as knowledge of sync lock. An article on the Java memory model with the thread of concern is how the virtual machine concurrency and concurrency control, the focus of this article is efficient concurrent . Paper structure is as follows:

  • Thread Safety
  • Thread-safe implementation
  • Lock optimization

Second, the security thread

Concurrency can make better use of computer resources, handle multiple tasks simultaneously. But first we need to ensure concurrency should be correctness, followed by the realization that efficient performance, concurrent validity involved thread-safe :

  • Defined : When multiple threads access an object, if you do not consider the scheduling of threads in the runtime environment and alternately perform and does not require additional synchronization, or anything else in the caller, calling the object's behavior can be get the right result, that this object is thread safe.

    Thread-safe code must have the characteristics : the code itself encapsulates all the necessary means to guarantee the correctness (such as mutual exclusion synchronization), so that the caller without having relations multithreading issues, but do not need to own any measures taken to ensure the correct call multithreaded .

  • Classification : In accordance with "level of security" from strong to weak can be divided into the following five categories:

    • . Ⅰ immutable : it must be thread-safe, external visible state will never change, will never see it in an inconsistent state among multiple threads.

      • If the shared data is a basic data types used when defining finalkeyword to ensure that it is immutable.
      • If the shared data is a target , you need to object variables are declared with the state final.
    • Ⅱ absolute thread safety. : Meet the above definition of thread safety level, that is, to achieve "regardless of the runtime environment, the caller does not need any additional synchronization measures" effect. But the absolute thread safety requirements are very stringent, even for such as built-in Java Vectorsclass can not achieve this absolute safety!

    • Ⅲ relative thread-safe. : Thread safety in the usual sense, it is necessary to ensure that an object individual operation is thread safe, when you call without having to do additional safeguards, but for continuous call some particular order, may still require additional synchronization means to ensure the correctness of the call. Most of thread-safe Java classes fall into this category, for example Vectors, HashTableand so on. 4

    • Ⅳ thread-compatible. : Itself is not thread-safe objects by using the right means to achieve synchronous thread-safe effect. The vast majority of class we normally use is compatible with belonging to a thread, for example ArrayList, HashMapand so on.

    • . Ⅴ thread-hostile : Whether synchronous measures, they can not use concurrent code in a multithreaded environment. Java language born with characteristics of multi-threading, thread-hostile rejection of this multi-threaded code is rare. ThreadClass suspend()and resume()method is a typical example of thread-hostile, but these methods have been abandoned.

      suspend () and resume () are used to suspend and resume threads when two threads at the same time hold a thread object, if a thread attempts to pause, another attempt to resume the thread, in the case of concurrent calls no matter when whether the synchronization, the target thread there is the risk of deadlock.


Third, thread-safe implementation

Thread-safe implementation and programming code that can be divided into internal virtual machine implementation, the focus of this article is to achieve a virtual machine, the following is the operation of the process virtual machine implementation thread-safe:

  • . Ⅰ synchronization mutex (Mutual Exclusion & Synchronization) :
    • Synchronization : when referring to multiple threads concurrently access shared data, ensure that the shared use of data is only one thread at a time.

    • Exclusive : is a means of synchronization, critical section, mutex and semaphores are the main way to achieve mutually exclusive.

      Mutex is because, if the synchronization; exclusive method, synchronization purposes.

    • Pessimistic strategy : mutually exclusive synchronization has become a blocking synchronization, concurrency strategy belong pessimistic that they do not do the right synchronous measures to see there will be a problem, regardless of whether there is competition really share data, it must be locked.

    • The Java implementation means :

      • Use synchronizedkeyword:
        • Principle : synchronizedAfter compilation, monitorenter and monitorexit bytecodes is formed around the instruction sync blocks. If you synchronizedexplicitly specify a locked object, then the object is locked; otherwise you will need to get the Class object corresponding to the object instance or modified in accordance with the method or class is an instance method as the lock object.
        • Process : When a monitorenter instruction, if the acquisition is successful, the object is not locked or the current thread already owns the lock object, the lock counter is incremented when the corresponding instruction execution monitorexit lock counter by 1, when the counter too, lock it will be released. If you acquire a lock fails, the current thread will block until the object until the lock is released by another thread.
        • Note : synchronizedKeyword is a reentrant lock will not lock yourself out of the situation. Sync block before executing the thread has entered, can block behind the thread enters. Since Java thread and is mapped onto the elements of the multithreaded operating system, and thus blocked thread wake-up operations are necessary to switch from user mode to kernel mode, such switching state takes a lot of time. Thus synchronizedbelongs to a heavyweight operation in Java.
      • Use ReentrantLockcategories:
        • ReentrantLockLikewise a reentrant locks, by providing lock()/unlock()to achieve synchronous operation method, and it synchronizedhas the following differences:
          • Interruptible wait : When the thread holding the lock release locks for long periods, waiting thread can choose to give up waiting, changed other things.
          • Fair lock : synchronizedis a non-fair locks, after that is blocked thread can first obtain the lock, may cause thread starvation. ReentrantLockThe default lock is unfair, but you can pass parameters fair locks changed by the constructor. In general unfair lock efficiency will be faster than the efficiency of the fair locks.
          • Binding multiple conditions : When using a synchronizedkeyword, the time and if you want more than one of the conditions associated with it, would have added extra locks. And ReentrantLockcan bind simultaneously multiple Conditionobjects, only you need to call multiple newCondition()methods can be.
      • On ReentrantLockand synchronizedselected: promote the use of synchronizedkeywords, although it belongs to the heavyweights lock, but Java has done a considerable amount of its optimization for improved performance.
  • . Ⅱ nonblocking synchronization (Non-Blocking Synchronization) :
    • Optimistic strategy : non-blocking synchronous belong to an optimistic strategy, which is based collision detection, which is to operate, if no other thread contention data, the operation was successful; if there is contention for shared data, resulting in conflict, to take other compensation measures, the most common measure is to continue to retry until it succeeds.
    • Since the operation and conflict detection operations are atomic requires, hence the need for hardware instruction set:
      • Test and set (Test-and-Set)
      • Obtain and increase (Fetch-and-Increment)
      • Swap (Swap)
      • Compare and swap (Compare-and-Swap, CAS)
      • Comparison of link / storage conditions (Load-Linked / Store-Conditional)
  • . Ⅲ No synchronization scheme :
    • Meaning : we must ensure thread safety does not necessarily synchronize, synchronize only means of ensuring the accuracy of data shared during the race. If a method should not involve sharing of data, you do not need any synchronization to ensure its accuracy. Here are two examples:
    • Reentrant code may be (a Reentrant Code) :
      • Meaning : reentrant code is also known as pure code (Pure Code), it can be interrupted at any time specified in the code to execute another piece of code, and after returning control, the original program without error.
      • Common features : data independent or common resources in the system heap memory, the state quantity is used by the incoming parameters, the method does not call non-reentrant like.
      • Analyzing principle : if the method returns a result is predictable, as long as the same data can be inputted return the same results, then it is reentrant code.
    • Thread Local Storage (the Thread Local Storage) :
      • Meaning : If the visible range is limited to sharing data within the same thread, no synchronization also ensures data contention does not occur between the threads.
      • Achieved : by ThreadLocaltype for local storage function, its interior is a Map, by ThreadLocal.threadLocalHashCodestoring key value KV, thread-local variable to the value of local variables, each thread has a unique ThreadLocal.threadLocalHashCodevalue, using this value It can retrieve the corresponding thread-local variables. Android asynchronous message processing mechanism of Looperan object is by ThreadLocalway of storage.

Fourth, the lock optimization

Lock concurrent optimization is to achieve efficient and more efficiently share data between threads and resolving competition problems, thereby improving the performance of programs, the following are five kinds of locks optimization techniques:

  • . Ⅰ adaptive spin (Spinning the Adaptive) :

    • Background : In many applications, data sharing locked state only for a short period of time, this time in order to suspend and resume the thread is not worth it, because the two operations are related to the state of the switch, it consumes resources.
    • Principle : If the physical machine has more than one processor, allows two threads execute in parallel, you can let the thread behind the request are busy loop waiting (spin), do not give up the processor execution time, take a look at holding the lock whether the thread will soon release the lock, this technique is known as spin locks .
    • Optimization : introducing adaptive spinlock after JDK1.6, spin time is no longer fixed. A lock on the same object spin-wait just successfully won lock will let spin the thread last longer; and if the spin lock for a little too successful, that after acquiring the lock when the spin-off process may be omitted to avoid a waste of processor resources.
    • Note : Spin itself without the overhead of thread switching, but it is to take up processor time, so the spin lock wait time is short for spin thread to show its effect, if you wait too long, spin only You can waste processor resources. Therefore, the spin lock and not a substitute for obstruction.
  • . Ⅱ eliminate lock (Lock Elimination) :

    • Background : running on a virtual machine in-time compiler when, for some of the code requires synchronization, but was detected in the case of shared data competition can not exist.
    • Judgment basis : Escape data to support analysis. If all data is a piece of code in the heap will not escape out to be accessed by other threads, they can be treated as data on the stack, namely private thread, no synchronization lock.
  • . Ⅲ lock coarsening (Lock Coarsening) :

    • Background : In principle, we always recommend to limit the scope of sync blocks as small as possible, that is, only to be synchronized before the actual scope of shared data, but if a series of successive operations are repeatedly lock and unlock the same object even lock operation is present in the loop body, even if there is no thread contention, frequently mutex synchronization can also cause unnecessary performance loss.
    • Examples : In one method, a continuous call StringBuilderclass append()when the string concatenation method, there is such a virtual machine to detect a series of operations are fragmentary lock on the same object, the lock range will synchronize to the entire roughened external operations sequence, is about to expand to lock first append()before until last append()after so just lock once.
  • . Ⅳ lightweight lock (Lightweight Locking) :

    • Background : For most of the locks, are not competing in the entire synchronization cycle. Note that this is an empirical data.
    • Objective : for multi-threaded under the premise of no competition, reduce the use of traditional heavyweight operating system lock mutex produce performance overhead.
    • Object header (Header Object) : Mark Word object header portion is biased locking key lock and lightweight implementation, the HotSpot VM flag in its corresponding memory contents and the state shown in the following table:
    Memory contents Mark status
    The hash code of the object, the object generational Age 01 Unlocked
    Pointers to lock records 00 Lightweight lock
    Heavyweight locks pointer pointing 10 Expansion (heavyweight lock)
    Empty, no need to record 11 GC mark
    Bias thread ID, timestamp bias, the object generational Age 01 Be biased
    • Locking procedure : When entering the synchronized block of code, if this synchronization object is not locked (locked flag is 01), the virtual machine will first build a space record locks (Lock Record) in the current thread's stack frame to store the current Mark Word copy lock object, as shown below:
      Here Insert Picture Description
      then the virtual machine will try to use the CAS operation target of Mark Word updated to point to lock Record pointer, if the update operation is successful, then this thread would have the object lock, and lock objects mark Word flag will be converted to 00, it indicates that this object is lightweight locked state, as shown below:
      Here Insert Picture Description
      If the update operation fails, the virtual machine first checks the object of mark Word points to the current thread the stack frame, if it shows that the current thread already owns the lock object directly into the synchronized block is executed, otherwise explain this lock object has been preempted by other threads.
    • Unlock process : and the same locking procedure is completed by CAS operations. If Mark Word object is still pointing to the locks on the thread, we use CAS will replace back the object's current Mark Word and thread copy of Mark Word copy, if the replacement is successful, the entire synchronization process is complete; if the replacement fails, indicating other thread tried to acquire the lock, it would have to release the lock at the same time, wake suspended thread.
    • Precautions :
      • If more than two threads competing for the same lock, lightweight lock is no longer valid, it is to be expanded heavyweight lock, the lock state flag value becomes 10.
      • In the absence of competition, the CAS operation using lightweight lock avoiding the overhead of using the mutex, if there is competition, in addition to the additional overhead of the mutex CAS operation occurred, so in the case where the competition lightweight lock will be slower than the traditional heavyweight lock.
  • . Ⅴ lock biased (Biased Locking) :

    • Objective : to eliminate data synchronization primitives in the absence of competition, to further improve the operating performance of the program.
    • Principle : biased locking will be biased in favor of the first to get its thread, if in the next execution, the lock is not acquired by another thread, the thread holds the lock bias will never need to be synchronized.
    • Process : When a thread lock object is first acquired, the virtual machine will be the object header flag is set to 01, that is biased mode. At the same time the use of CAS operation to get to the lock thread ID is recorded in Mark Word object, if CAS is successful, the future holds a biased locking thread every time you enter the relevant sync block locks, virtual machines can no longer be any synchronization operations.
    • Note : When there is another thread to try to acquire this lock mode bias came to an end. The object is currently in the locked state, the state returns to the unlocked or locked after canceling bias lightweight, the subsequent synchronizing operation is executed as above and lightweight lock. Biased locking relationship and lightweight lock as shown below:
      Here Insert Picture Description
      biased locking can be synchronized with a number of questions but no competitive process performance, but if the program most of the locks are always a number of different threads access, that bias mode It is redundant. Use parameters -XX: -UseBiasedLocking prohibit biased locking optimization.

V. REFERENCE

Reference this article from:

  • "In-depth understanding of the Java Virtual Machine"
    • Part V efficient concurrent
      • Security and lock optimization Chapter 13 Java threads

Points refining | understanding of the JVM thread-safe lock & Optimization

Guess you like

Origin blog.csdn.net/qq_38182125/article/details/90812394