Java concurrent programming art note Chapter 3 (1), java chapter 3

Source: Internet
Author: User

Java concurrent programming art note Chapter 3 (1), java chapter 3

1. Java threads communicate with programmersTransparent, But itsMemory visibility problemsYesCauseOther weird issues;

2. Two key issues need to be considered in parallel programming: 1. HowCommunication2. How are threads?Synchronization

3. In imperative programming, there are two types of communication between threads:1. Shared Memory 2. Message Passing

4.Shared Memory-> Threads share the public state of the program and perform implicit communication by reading and writing the public state in the memory.

5.Message transmission-> There is no public status between threads, and the communication between threads must be displayed by sending messages;

6. synchronization is a mechanism that controls the relative sequence of operations between different threads. In the shared memory concurrency model, operations are displayed and performed, the programmer needs to display a specified method or code that must be mutually exclusive between threads. In the message transmission concurrency model, the synchronization is implicit because the message must be sent before it is accepted...

7.Shared Memory Model Used by JavaBut Java threads communicate implicitly. The whole process is transparent to programmers, so they cannot understand implicit thread communication,Memory visibility issues...

8. Java inter-thread communication is controlled by the Java Memory Model (JMM,JMMDetermines when a thread writes shared variables to another thread...

9. JMM defines the abstract relationship between threads and memory:Shared variables between threads are stored in the Main Memory. Each thread has a private Local Memory ), the local memory stores copies of variables read and written by the thread;

10. ActuallyLocal MemoryYes JMMAbstractionOneConceptIt does not actually exist. It covers cache, write cache, registers, and other hardware and compiler optimization.

11. Communication between thread A and thread B requires two steps:

A. Thread A refreshes the updated shared variables in local memory A to the master memory;

B. Thread B goes to the main memory to read the shared variables updated before thread.

Summary: The communication between thread A and thread B must go through the main memory. By controlling the interaction between the main memory and the local memory of each thread, JMM asks the Java programmer to provide the memory visibility guarantee...

12. To improve the performance, the compiler and processor usually reorder the commands, which are divided into the following three types:

A.Compiler-optimized re-sortingYou can reschedule the statement execution sequence without changing the single-thread semantics;

B.Command-level parallel re-sortingIf there is no data dependency, the processor can change the execution sequence of the statements corresponding to the machine commands;

C.Memory System re-sortingBecause the processor uses the cache and read/write buffer, the loading and storage operations are out of order.

Summary: A is the re-sorting of the compiler, while bc is the re-sorting of the processor. For the compiler, JMM will disable the specific re-sorting of the compiler (for example, when no data dependency exists). JMM will re-sort the processors, insert a specific type of Memory Barriers)

13. Common processors (x86 processors) onlyStore-Load(Store1 first refreshes the data to the memory, and then installs the command after execution ...) the ARM architecture is odd. It may support Load-Load, Load-Store, and Store-Store;

14.Happens-before-> The JSR-133 uses this concept to describe memory visibility between operations;

15. happens-before rules:

A.Program sequence rules: For each operation of a thread, happens-before performs other operations after the thread

B.Monitor lock rules: Unlock a lock. happens-before locks the lock later;

C.Volatile variable rules: Write of a volatile domain. The happens-before reads the volatile domain in any backward order.

D.Transmission: A happens-before B, B happens-before C => A happens-before C;

Summary:Happens-before is a rule that shows programmers to understand the underlying reordering of JMM....

16.As-if-serial-> No matter how the program is rearranged, the execution result of a Single-threaded program cannot be changed... as-if-serial is to protect the single-threaded program.

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.