Java Memory models and threads

Source: Internet
Author: User

Written in front: Unlike the memory models used by the mainstream programming language (c/C + +, etc.) to directly use physical hardware and operating systems, Java virtual machines Define a Java memory model in order to block memory access differences between various hardware and operating systems. It mainly defines the access rules for each variable in the program (the underlying details of storing variables in the virtual machine into memory and removing variables from memory).


Interaction between threads, main memory, and working memory 1.java memory model structure:

-All variables are stored in main memory.

-Each thread also has its own working memory.

-the variable to be used by the thread that was copied from main memory in the working memory

-Each thread must operate on a variable in its own working memory, cannot manipulate the main memory directly, or manipulate the working memory of other threads, and the threads need to pass through main memory.

2. Inter-memory Interoperability:

The Java memory model defines the following eight actions for the specific interaction protocol between main memory and working memory, that is, how a variable is copied from main memory to working memory, how to synchronize from working memory to main memory.

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

Unlock (Unlocked): Acts on the main memory variable, releasing a variable that is in a locked state, and the released variable can be locked by another thread.

READ: Acts on the main memory variable, transferring a variable value from main memory to the working memory of the thread for subsequent load actions to use

Load: A variable that acts on working memory, which places the value of a read operation from the main memory into a variable copy of the working memory.

Use: 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 (Assignment): A variable acting on a working memory that assigns a value to a working memory from the execution engine, and performs this operation whenever the virtual opportunity is assigned to a byte-code instruction that assigns a value to a variable.

Store: 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: 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.


Inter-memory Interaction flowchart

Related rules:

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

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

A thread is not allowed 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 main memory, and it is not allowed to use a variable that is not initialized (load or assign) directly in working memory. That is, the assign and load operations must be performed before a variable is implemented with the use and store operations.

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

If you perform a lock operation on a variable, the value of the variable in the working memory will be emptied, and the value of the variable will need to be re-executed before the execution engine uses the variable, either the load or the assign operation.

If a variable is not locked by the lock operation beforehand, it is not allowed to perform a unlock operation on it, nor is it allowed 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 3.volatile variables

1. Two types of features:

-guarantees the visibility of the variable for all threads.

-Disables command reordering optimizations.

2. Make the Java memory model have: order, visibility, atomicity

4. Threading implementation

1. One-to-one (1:1) kernel-level threading model:

In a one-to-one model, each user thread corresponds to its own kernel dispatch entity. Each thread is dispatched by the kernel and can be dispatched to other processors. Of course, the result of scheduling by the kernel is that each operation of the thread will switch between the user state and the kernel state. In addition, the kernel maps scheduling entities for each thread, which can have an impact on system performance if a large number of threads are present. However, the model is more practical than many-to-one threading model.

2. Multiple-to-one (m:1) user-level threading model:

In a multi-threading model, all the details of thread creation, scheduling, and synchronization are all handled by the process's user space line libraries. Many of the operations of the user-state thread are transparent to the kernel because the kernel does not need to be taken over, which means that the kernel state and the user state are not required to switch frequently. Threads are created, dispatched, and synchronized very quickly. Of course, some of the other operations of the thread go through the kernel, such as IO Read and write. This leads to a problem: when multithreading is executing concurrently, if one of the threads performs an IO operation, the kernel takes over the operation, and if the IO is blocked, the other threads of the user state are blocked because the threads correspond to the same kernel scheduler entity. On multiprocessor machines, the kernel does not know that the user state has these threads, cannot dispatch them to other processors, and cannot be dispatched by priority. There is no point in using this thread!

3. Multi-to-many (m:n) two-level threading model:

Many-to-many models combine the advantages of 1:1 and m:1 to avoid their drawbacks. Each thread can have more than one dispatch entity, or multiple threads can have one scheduled entity. Sounds perfect, but thread scheduling needs to be implemented by the kernel State and the user configuration. It is conceivable that when multiple objects operate on one thing, there must be some other synchronization mechanism. The Division of User-State and kernel-state results in a complex implementation of the model. NPTL once wanted to use the model, but it was too complex to make a large-scale change to the kernel, so it was a one-to-one model.

5.java Thread Scheduling

1. Synergy:

Definition: The thread does not switch after it has done its thing

Pros: Simple to implement, no synchronization required

Cons: Unstable, low fault tolerance, which can cause a system crash once a wired thread is blocked

2. Preemptive (Java use):

Definition: The system allocates execution time according to the high and low priority level

Pros: No system crashes due to a thread problem

Cons: Need to be synchronized, complex

Status of 6.java threads

1. NEW: A thread that has not been started since it was created

2. Run (rannable): includes the operating system thread state in running (executing) and ready (waiting for CPU allocation execution time).

3. Wait (Waiting):

Wait indefinitely: The CPU is not allocated execution time until it is woken up by other threads (Object.wait (); Thread.Join (); Locksupport.park ())

Deadline wait: The CPU will not be allocated execution time, no wait for the other threads to wake up, a certain time by the system automatically wake.

(Thread.Sleep (); object.wait (long timeout); Thread.Join (long timeout); Locksupport.parknanos (); Locksupport.parkuntil ())

4. Blocking (Blocked): Waiting to get to an exclusive lock. The thread will enter this state when the program waits to enter the synchronization area.

5. End (Terminated): Thread state of the terminated thread. The thread has finished executing.

Java Learning Exchange QQ Group: 523047986 prohibit small talk, non-happy do not enter!

Java Memory models and threads

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.