In-depth understanding of Java virtual Machines-efficient concurrency reading notes

Source: Internet
Author: User
Tags visibility

Java memory models and Threads

Overview

Multitasking is almost a necessary function in modern computer operation system, multi-task operation is a pressing means, like Windows , we squeeze it to run a number of tasks, both high and play. Concurrency is another more specific application scenario. The number of things processed per second (transactions per Second,tps) is the most important indicator. Developers should understand and use concurrency.

The efficiency and consistency of the hardware

In addition to software concurrency, physical computers also have concurrency problems. The computer's storage device and processor operation Speed have several orders of magnitude difference, modern computers have to add a layer of cache as a buffer between memory and processor, which can improve processing speed. Based on cache, the speed contradiction between processor and memory is solved, but the complexity of computer system is improved, which brings the problem of cache consistency. In multiprocessor systems, each processor has its own cache, and they share the same main memory.

when a task of multiple processors involves the same block of main memory, it may cause the respective cache data to be inconsistent. In order to solve this kind of consistency problem, it is necessary to follow some protocol when each processor accesses the cache, to operate according to the Protocol, such protocol has MSI,MESI (Illinois protoclo), MOSI,Synapse,Firefly and Dragon protocal and so on.

In addition to increasing the cache, for the processor to be fully utilized, the processor may perform random execution of the input code ( Out-of-order execution) optimization, the processor will then reorganize the results of the order execution, ensuring consistency with the sequential execution results, and not guaranteeing that the order of the individual statements is consistent in the order of the input code. The result is that a computational task relies on an intermediate result of another computational task, whose ordering cannot be saved by the order of the Code. similar command reordering (instruction Reorder) optimizations exist in Java.

Java memory Model

The Java Virtual Machine specification attempts to define a Java memory model that blocks memory access differences between various hardware and operating systems to enable Java Programs can achieve consistent memory access across a variety of platforms. Defining the memory model is not an easy thing to do and be sufficiently rigorous enough to be loose. Rigor is not ambiguous for concurrent memory operations, loose is to have enough space to take advantage of the features of various hardware.

Main memory vs. working memory

The Java memory model specifies that all variables are stored in main memory and that the main memory is part of the Java virtual machine memory, and each thread has its own working memory. The working memory of the thread holds a copy of the Master memory copies of the variables used by the thread, and all the operations of the thread on the variable must be made in working memory and not directly manipulate the main memory. Separate threads from each other. The transfer of variable values between threads needs to be done through main memory. The three relationships are as follows:

here the main memory, working memory and Java heap, stack, and method areas of the Java memory area are not the same level of memory partitioning.

Inter-memory interaction operations

The Java memory model defines the following 8 operations for the specific interaction protocol between main memory and working memory .

Lock (Locked): A variable that acts on the main memory and identifies a variable as a thread-exclusive state.

unlock(unlock): acts on the main memory variable, releasing a variable that is locked, and the released variable can be locked by another thread.

read(reading): acting on the main memory variable, A variable value is transferred from main memory to the working memory of the thread for subsequent load actions to use

load(load): A variable acting on a working memory that places the value of a read operation from the main memory into a variable copy of the working memory.

Use (using): A variable that acts on the working memory, passing a variable value in the working memory to the execution engine, which is performed whenever the virtual opportunity is to a bytecode instruction that needs to use the value of the variable.

assign(assigned value): A variable acting on a working memory that assigns a value from the execution engine to the working memory, and performs this operation whenever the virtual opportunity is assigned to a byte-code instruction that assigns a value to a variable.

store(storage): A variable acting on a working memory that transfers the value of a variable in the working memory to the main memory for subsequent write operations.

write(writes): A variable that acts on the main memory, which transfers the store operation from the value of a variable in the working memory to a variable in the main memory.

If you want to copy a variable from the main memory to the working memory, you will need to follow the searchReadand theLoadoperation, if the variables are synchronized back to the main memory from the working memory, they should be executed sequentiallyStoreand theWriteoperation. Javathe memory model only requires that the above operations must be executed sequentially, without guarantee that it must be executed consecutively. AlsoReadand theLoadbetween,Storeand theWritecan be inserted between other directives, such as variables in main memorya,bwhen accessing, the possible order isread a,Read B,Load B,Load a. JavaThe memory model also specifies that when performing the eight basic operations above, the following rules must be met:

① does not allow one of the read and load,store , and write operations to appear separately

② does not allow a thread to discard its most recent assign operation, that is, the variable must be synchronized to main memory after it has changed in working memory.

③ does not allow a thread to synchronize data from the working memory back to main memory for no reason (no assign operation has occurred).

④ A new variable can only be born in the main memory, and does not allow direct use of a variable that is not initialized (load or assign) in working memory. That is , you must execute the assign and load before implementing the use and store operations on a variable. operation.

⑤ A variable allows only one thread to lock it at the same time ,andlock and unlock must appear in pairs

⑥ If you perform a lock operation on a variable , the value of this variable in the working memory is emptied, and the load or assign needs to be re-executed before the execution engine uses this variable operation initializes the value of the variable

⑦ If a variable is not locked by the lock operation in advance , it is not allowed to perform unlock operations on it, Nor is it allowed to go to unlock A variable that is locked by another thread.

⑧ before performing a unlock operation on a variable , you must first synchronize this variable into main memory (perform store and write operations).

Special rules for Volatitle-type variables

The keyword volatitle is a lightweight synchronization mechanism provided by the Java Virtual machine, which is difficult to understand correctly and completely. Understanding The Volatitle keyword is useful for understanding other characteristics of multithreaded operations. The variables defined by the Volatitle keyword have 2 characteristics:

The first guarantees the visibility of this variable to all threads.

The second is to prohibit command reordering optimizations.

The Volatitle variable can only guarantee visibility, and in an operation scenario that does not conform to the following 2 rules, we still need to lock to ensure atomicity:

The ① operation results do not depend on the current value of the variable, or can guarantee that only a single thread modifies the value of the variable.

The ② variable does not need to participate in the invariant constraint with other state variables.

The key change to the Volatitle modified variable is that it performs a "lock Addl $0x0, (%ESP)" operation, which is equivalent to a memory barrier (MemoriesBarrier or memory Fence, reordering can not reorder the following commands before they are placed in the Ram barrier position)

Why Choose volatitle (under certain circumstances )?

performance consumption in read operations is almost indistinguishable from normal variables and is slower in write operations. There is a lot of elimination and optimization of the virtual machine to lock. The Volatitle keyword is somewhere between the normal variable and the lock.

Special rules for long and double types of variables

JavaMemory ModelLock,Unlock,Read,Load,Assign,User,Store,Writeit8all operations are atomic, butJavathe memory model will not beVolatileDecoration of -bits of data are divided into two reads and writes +for the operation, so that, multithreading concurrency, there will be threads may read to the "half variable" value, however, this is very rare, the current platform of commercial virtual machines almost all choose to -the read and write of bits are implemented as atomic operations to implement specifications.

Atomicity, Visibility and ordering

Atomic nature: atomicity means that in one operation the CPU is not allowed to pause and then dispatch, neither interrupted nor executed.

Visibility of: visibility means that when a thread modifies the value of a thread-shared variable, other threads can immediately know the change.

Order: the natural ordering of the programs in the Java memory model can be summed up in one sentence: If you observe in this thread, all operations are orderly, and if you observe another thread in one thread, all operations are unordered. The first half of the sentence refers to " line range expressed as serial semantics ", the latter sentence refers to the " order reordering " phenomenon and " main memory synchronization delay in working memory " phenomenon.

The principle of antecedent occurrence ( Happens-before)

The principle of antecedent occurrence ( Happens-before) is the main basis for judging whether the data is competitive and whether the thread is secure or not. The first occurrence is the partial-order relationship between the two operations defined in the model in Java memory, if Operation A precedes action B, then the Operation A the impact can be manipulated B observed.

Natural pre- existing relationship in the Java memory model:

1. Program Order rules: Within the same thread, in the order in which the code appears, the preceding code precedes the code, which is exactly the control flow order, because the branching and looping structures are taken into account.

2. pipe Lock rule: a unlock operation takes place in the back (time) lock operation on the same lock .

3. Volatile variable rule: The write operation of a volatile variable takes place at the back (time) of the read operation of the variable.

4. Thread Initiation rule: the start () method of the thread takes precedence over each operation of this thread.

5. thread Termination rule: All operations of the thread are prior to termination detection for this thread. the termination of a thread can be detected by means of the end of the Thread.Join () method,The return value ofthread.isalive () , and so on.  

6. thread Break rule: the call to the thread interrupt () method first occurs when the code of the interrupted thread detects that the interrupt event occurred, and can be thread.interrupt () method to detect if a thread is interrupted

7. object Termination rule: Initialization of an object is done prior to the beginning of the Finalize() method in which it occurred.

8. transitivity: If Operation A precedes operation b, operation b precedes operation C, then the operation A precedes operation C.

Summary: An operation "occurs first" does not mean that the operation occurs first, and an operation antecedent does not mean that the operation takes place in time (the appearance of reordering). The order of time is not much related to the first occurrence, so the measurement of concurrency security issues should not be affected by the time sequence, all the first occurrence of the principle prevail.

In-depth understanding of Java virtual Machines-efficient concurrency reading notes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.