Deep understanding of Java Virtual Machines-12th Chapter Java memory model and threading

Source: Internet
Author: User

12th Java memory model and threading

Java memory model

Main memory vs. working memory:

The Java memory model specifies that all variables are in main memory, and each thread has its own working memory.

In working memory, a copy of the primary memory that is used by the thread is saved, and all operations on the variable must be made in working memory.

Inter-memory interaction operations:

1 lock A variable that acts on the main memory, which identifies a variable as a thread-exclusive state.

2 unlock a variable that acts on the main memory, releasing the locked variable

3 read a variable that acts on the working memory, transferring the value of a variable from main memory to the working memory of the thread.

4 load a variable that acts on the working memory, which puts the value of the variable that the read operation obtains from the main memory into a variable copy of the working memory

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

6 assign a variable that acts on the working memory, assigns a value received from the execution engine to the working memory

7 Store A variable that works on a day's memory, transferring the value of a variable in the working memory to the main memory.

8 Write a variable that acts on the main memory, placing the value of the store operation's variable from the working memory into main memory

When a variable is defined as volatile, it will have two characteristics

1 Ensure that this variable is visible to all threads and is still not thread-safe for non-atomic operations.

2 Prohibit command reordering optimizations.

Atomicity, Visibility and ordering

First-mover principle

State transitions for Java threads

Newly created new new is in this state along with the non-started purebred

Running Runnable includes running and ready in the operating system thread state, a thread in this state may be executing, or it may be waiting for the CPU to allocate execution time to it

Threads that wait indefinitely for waiting to be in this state are not allocated CPU execution time, they wait for the other line to wake up Cheng: There is no object.wait () method to set the timeout parameter; the Thread.Join () method without setting the timeout parameter ; Locksupport.park () method

Wait for Timed waiting the thread in this state will not be allocated CPU execution time, but it will not wait for the line to wake up Cheng, and after a certain period of time they are automatically awakened by the system: Thread.Sleep () method , set the Object.wait () method of the timeout parameter, set the Thread.Join () method of the Thimeout parameter; Locksupport.parknanos () method; Locksuport.parkunitl () method

Blocking Blocked waiting to acquire an exclusive lock

End Terminated thread state of terminated thread

Deep understanding of Java Virtual Machines-12th Chapter Java memory model and threading

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.