Java Interview Collection-----Java memory model and inter-memory interactive operation __java

Source: Internet
Author: User

This September to find a job, inadvertently see a blog, think of the knowledge points in the summary, consolidate the basic knowledge, to find work to do some preparation:
Overview of the Java memory model http://blog.csdn.net/pistolove/article/details/46753275 Java memory model Inter -memory interoperability

memory model concept: masks Memory access differences for various hardware and operating systems to achieve a consistent memory access for Java programs under a variety of platforms.

Java memory model goal: defines access rules for variables in a program, namely, the underlying details of storing variables in a virtual machine in memory and fetching variables from memory. rules for the Java memory model
All variables are stored in the main memory. Each thread has its own working memory. There is no direct access to the variables in each other's working memory between threads, and the transfer of variable values between threads needs to be done through main memory. Attention Matters
The variables described above include instance fields, static fields, and elements that make up an array object, but do not include local variables and method parameters (thought to be thread-private and not shared). The thread's working memory stores a copy of the main memory of the variable used by the thread, which must be done in working memory and not read directly from the variables in main storage. The main memory, working memory and Java heap, stack, method area in Java memory area are not the same level of memory partition, which is basically not related. Inter-memory interactive exercise

There are two questions:
1. How to copy a variable from main memory to working memory.
2. How to sync back to main memory from working memory.
For both of these issues, 8 operations are defined in the Java memory model, and the virtual machine implementation must ensure that each of the operations mentioned below is atomic and cannot be divided (except the double, long type).8 kinds of Operation
Lock (Lock): A variable that acts on the main memory and identifies a variable as a thread-exclusive state. Unclock (unlock): A variable that acts on the main memory, releasing a variable in a locked state and releasing the variable to be locked by another thread. Read (read): A variable that acts on the main memory, transferring the value of a variable from main memory to the working memory of the thread so that subsequent load actions are used. Load (load): A variable that acts on the working memory, putting the value of the read operation from the main memory into a variable copy of the working memory. Use: A variable that acts on working memory, passing the value of a variable in working memory to the execution engine. Assign (Assignment): A variable that acts on working memory, assigning the value received by the execution engine to a variable in working memory. Store: A variable acting on working memory that transmits the value of a variable in working memory to the main memory for subsequent write operations. Write (write): A variable that acts on the main memory, putting the value of a variable in the store operation from working memory into a variable in the main memory.Operational Requirements
One of the read and load, store, and write operations is not allowed to appear alone, that is, a variable is not allowed to read from main memory but is not accepted by working memory, or when the working memory initiates a writeback but the main memory is not accepted. A thread is not allowed to discard its most recent assign operation, that is, the variable must be synchronized to the main memory after it has changed in working memory. A thread is not allowed to synchronize data from the working memory of the thread to the main memory without any assign (no action taken). 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, in other words, you must perform the Assign and load operations before using a variable, store operation. A variable at the same time allows only one thread to lock it, but the lock operation can be repeatedly executed by the same thread multiple times, after the lock is executed more than once, the variable is unlocked only if the same number of unclock operations are performed. If you perform a lock operation on a variable, the value of the secondary variable for the working memory is emptied, and the value of the load or assign operation initialization variable needs to be rerun before the execution engine uses the variable. If you perform a lock operation on a variable, the value of the secondary variable for the working memory is emptied, and the value of the load or assign operation initialization variable needs to be rerun before the execution engine uses the variable. Before you can perform unclock on a variable, you must first synchronize the variable back into main memory (performing store, write operations).

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.