Deep understanding of Java Virtual Machine-Chapter 6 Java Memory Model and thread, and Chapter 4

Source: Internet
Author: User

Deep understanding of Java Virtual Machine-Chapter 6 Java Memory Model and thread, and Chapter 4

Chapter 2 Java Memory Model and thread

 

Java Memory Model

Master memory and working memory:

The java Memory Model specifies that all variables are in the main memory, and each thread has its own working memory.

The worker memory stores copies of the master memory used by the thread. All operations on variables by the thread must be performed in the worker memory.

Interactive operations between memory:

1 lock acts on the main memory variable. It identifies a variable as a State exclusive to a thread.

2. unlock acts on the main memory variable and releases the locked variable.

3. read acts on the variable in the working memory and transfers the value of a variable from the main memory to the working memory of the thread.

4 load acts on the variable in the working memory. It puts the variable value obtained from the master memory by the read operation into the variable copy in the working memory.

5. use variables acting on the working memory pass the value of a variable in the working memory to the execution engine.

6. assign acts on the variable in the working memory and assigns a value received from the execution engine to the variable in the working memory.

7 store serves as a variable in the daily working memory and transfers the value of a variable in the working memory to the main memory.

8. write acts on the main memory variable and puts the value of the variable obtained by the store operation from the working memory into the main memory.

When a variable is defined as volatile, it has two features:

1. ensure the visibility of this variable to all threads. Non-atomic operations are still not thread-safe.

2. disable command re-sorting optimization.

Atomicity, visibility, and orderliness

FIFO Principle

Status conversion of Java threads

The New is in this status along the fl that has not been started.

Running Runnable includes Running and Ready in the operating system thread state. A thread in this state may be executing or waiting for the CPU to allocate the execution time for it.

Threads Waiting for Waiting in this state indefinitely will not be allocated the CPU execution time. They need to wait for other threads to explicitly wake up: objects without the Timeout parameter set. wait () method; no Thread with the Timeout parameter set. join () method; LockSupport. park () method

Threads Waiting for Timed Waiting in this status within a time limit will not be allocated CPU execution time, but do not have to wait for the threads to explicitly wake up. after a certain period of time, they will be automatically wake up by the system: thread. sleep () method; specifies the Object with the Timeout parameter. wait () method; sets the Thread of the Thimeout parameter. join () method; LockSupport. parkNanos () method; LockSuport. parkUnitl () method

Blocking Blocked waits for an exclusive lock to be obtained.

End the thread status of the Terminated thread

 

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.