Java Multithreading Knowledge Comb 1

Source: Internet
Author: User

Concept

JMM: It is stipulated that the JVM has main memory and working memory (working memory), all the class instances, static data and so on, which are shared by multiple threads. While working memory is the variable that the thread copies from the main memory and the local variables obtained by the access method, the other threads that are private to each thread are inaccessible, and each thread operates on the variable by copying it from main memory to working memory and then manipulating it. Data communication between multiple threads cannot be communicated directly to each other, only through shared variables.

Operation Instruction: JLS defines the operation instruction of thread to main memory:read,load,use,assign,store,write. These behaviors are non-decomposed atomic operations, relying on each other, read-load from the main memory to the current working memory, use-assign executing code to change the shared variable value, store-write with working memory data to refresh main memory related content. Reference http://www.cnblogs.com/fguozhu/articles/2657904.html

Process: Generally as the organizational unit of the resource, is the running instance of the computer program, indicating the executing instruction, has its own independent address space, contains the program content and the data, the inter-process resources and the state is isolated from each other

Threads: The execution process of the program, the basic unit of CPU dispatch execution, has its own program counters, registers, stacks, frames, share the same process address space, memory and other resources

Principled operation: The basic unit of CPU scheduling is instruction, while a program may be broken down into multiple instructions, such as i++, read-Modify-write 3, thread-safe premise is to ensure the operation of atomicity. The CPU does not perform thread scheduling and context switching during the execution of an instruction, but a thread switchover may occur in the execution gap of two instructions

Thread insecure: When a composite operation occurs, such as checking for multiple instructions, such as execution or read-modify-write, and cannot be performed atomically, you must rely on luck (the thread alternates with the appropriate timing), otherwise incorrect results will occur. Stateless objects (which do not contain any domains) are thread-safe

Built-in lock: synchronized a synchronous code block or method, which is a mutex that can only be held by one thread at a time. The built-in lock can be re-entered, that is, a thread can acquire a lock that it has already held, example:

Can be re-entered lock:

Visibility: multi-threaded communication using shared memory can cause problems with visibility, where changes made by one thread are not visible to other threads, causing other threads to still use the wrong values.

Volatile:java's lightweight synchronization measures, volatile can only guarantee multi-threaded memory visibility, does not guarantee the execution of multi-threaded order. The implementation principle is any variable modified by the volatile, do not copy the copy to the working memory, any changes are written in memory in time. As a result, changes to Valatile modified variables are immediately visible to all threads, but volatile does not guarantee that changes to variables are ordered. For the volatile variable to provide ideal thread safety, the following two conditions must be met:

1, the write operation to the variable does not depend on the current value.

2. The variable is not included in the invariant with other variables.

Synchronized: Visibility is guaranteed to be orderly and atomic in nature. Implemented as follows

Visibility: When a lock is released, changes to the shared variable are written directly back to main memory from the CPU cache, and when the lock is fetched, the CPU's cache memory is set to a stale state, and the shared variable value is re-read from main memory.

Ordering: When the compiler handles synchronized blocks of code, it does not move the code contained in it beyond synchronized, thus avoiding problems caused by code rearrangement.

Atomicity: Before a thread runs execution of a synchronized code block, it needs to get the lock object on the corresponding monitor and automatically release it after execution. Ensure that only one thread at a time is working on a variable inside the synchronization block.

The JVM performs the following steps:

    1. The object lock is obtained (the lock of the normal method is the this object, and the static method is the class object of the classes) and locks it (lock).
    2. Copies the required data from the main memory to its own working memory (read and load).
    3. Read or modify the corresponding variable value (use and assign) according to the program flow.
    4. Copy the variable that modifies the value in your working memory back to main memory (store and write).
    5. Releases the object lock (unlock).

Java Multithreading Knowledge Comb 1

Related Article

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.