Java Memory models and Threads _ Learning Notes

Source: Internet
Author: User

In-depth understanding of Java Virtual machines:

1. Java Memory model

The Java Virtual Machine specification attempts to define a Java memory model. Java Memory Model (JMM)

1.1 Main memory vs. working memory

The Java memory model stipulates that all variables are stored in main memory (main memories).

Each thread also has its own working memory (working memories), and the working memory of the thread holds the main memory copy of the variable used by the thread, and the thread-to-variable operation is in working memory and cannot be read directly to the variables in main memory.

1. 2 memory See interactive operation

Special rules for 1.3 volatile variables

Guaranteed visibility of all threads

Prohibit command rearrangement optimization

1.4 Special rules for long and double types of variables

JMM requires 8 operations in 1.2 to be atomic, but for 64-bit data types (long double)

Allows the virtual machine to divide the read and write operations of 64-bit data that are not volatile modified into two 32-bit operations.

That is, the 64-bit data type is not guaranteed to load store read Write atomicity of these 4 operations.

All when multiple threads share variables of type long or double that do not declare bit volatile, some threads read the value of "half variable".

1.5 Atomic Visibility Ordering

Atomicity: Operation between read,load,assign,use,write,synchronized

Visibility: When a thread modifies a variable, other threads can immediately know the change.

Volatile,synchronized,final all have visibility

The visibility of a synchronization block is that before a variable executes a unlock, the variable must be synchronized back to main memory.

Order: If observed in this thread, all operations are ordered. If you look at another thread in one thread, all operations are unordered.

The first half of the sentence refers to: line range expressed as serial semantics

The second half of the sentence refers to: command rearrangement, working memory and main memory synchronization delay

1.6 Principle of antecedent occurrence

The time sequence and the principle of the first occurrence is not much related, so when we measure the concurrency security problem, do not receive the time sequence of interference, everything must be in accordance with the first occurrence principle prevail.

2 Java and threading

2.1 Implementation of Threads

The line program is more lightweight than the process of scheduling execution units, the introduction of threads, can be a process of resource allocation and execution of scheduling separate, each thread can share process resources (memory address, file I? O), and can be independently dispatched. (the basic unit of the thread program CPU Dispatch)

There are three ways to implement threads:

1. Implement with kernel thread

Kernel thread Kernel-level THREAD,KLT threads that are supported directly by the operating system (Kernel). The kernel dispatches threads through the Manipulation Scheduler (Scheduler) and maps the threads ' tasks to individual processors.

Multithreaded cores are called multithreaded kernels multi-threads kernel

The program does not use kernel threads directly, and it uses a high-level interface of kernel threads---lightweight process, light Weight process LWP

With the support of kernel threads, each lightweight process becomes an independent dispatch unit, and a lightweight process is blocked in the system in a timely manner and does not affect the entire process.

Limitations: Based on kernel thread implementations, various threading operations, such as creation, destruction, and synchronization, require system calls.

The cost of the system call is higher, and it needs to be switched back and forth between the user mode and the kernel state (Kernel mode). Second, lightweight processes require a certain amount of kernel resources, so the number of lightweight processes supported by a system is limited.

2, using the user thread implementation

Broadly speaking, a thread can be considered a user thread as long as it is not a kernel thread, and from this definition the lightweight process is also a user process.

In the narrow sense, the user thread refers to the line libraries which is completely built on the user space, and the system kernel cannot perceive the implementation of thread existence.

If the program is implemented properly, this thread does not need to switch to the kernel state, so the operation can be very fast and low-consumption, can also support a larger number of threads, some high-performance database multithreading is a user thread implementation.

Because the operating system can only allocate processor resources to processes such as "How blocking is handled" and "how to map threads to other processors in a multiprocessor system", such problems are extremely difficult to resolve and even cannot be completed.

Thus, programs implemented using user threads are generally more complex.

3. Using the user line loads lightweight process hybrid implementation

The operating system provides a lightweight process that is supported as a bridge between the user thread and the kernel thread, so that the kernel provides thread scheduling and processor mapping, and the user thread's system calls are done through lightweight threads, reducing the risk that the entire process is completely blocked.

The number of user threads versus lightweight processes is not necessarily n:m

2.2. Implementation of Java Threads

2.3. Java Thread Scheduling

Thread scheduling refers to the process by which a system assigns a processor to a thread

Collaborative thread scheduling

Preemptive thread scheduling

The Java Thread Scheduler system is automated, but it is recommended to prioritize threads.

State transitions:

Waiting: Threads in this state are not allocated CPU execution time, they wait for the other threads to wake up.

Timed waiting: A thread in this state will not be allocated CPU execution time without waiting for other threads to show wake. The system will automatically wake up after a certain amount of time.

Blocked (blocking): The thread is in this state when the program waits to enter the synchronization area.

Java Memory models and Threads _ Learning 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.