Home  >  Article  >  Java  >  Java memory model and threads

Java memory model and threads

伊谢尔伦
伊谢尔伦Original
2016-11-26 09:28:001162browse

Java Memory Model

Main memory and working memory:

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

  The working memory stores a copy of the main memory used by the thread. All operations on variables by the thread must be performed in the working memory.

  Interaction between memories:

  1 lock acts on variables in main memory, which identifies a variable as a thread-exclusive state.

   2 unlock Acts on variables in main memory, releases locked variables

   3 read Acts on variables in working memory, transfers the value of a variable from main memory to the thread's working memory.

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

  5 use acts on a variable in the working memory and transfers the value of a variable in the working memory To the execution engine

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

  7 store acts on a variable in the working memory, transfers the value of a variable in the working memory to in main memory.

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

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

  1. Guarantee this variable Visibility to all threads is still not thread-safe for non-atomic operations.

  2 Disable instruction reordering optimization.

 Atomicity, Visibility and Orderliness

 First-come-first-serve principle

State transition of Java threads

 New New Create trailing edge and unstarted purebreds are in this state

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

  Waiting indefinitely Waiting Threads in this state will not be allocated CPU execution time, they have to Waiting to be explicitly awakened by other threads: Object.wait() method without setting Timeout parameter; Thread.join() method without setting Timeout parameter; LockSupport.park() method

  Timed Waiting is in this state Threads will not be allocated CPU execution time, but there is no need to wait for other threads to be explicitly awakened. They will be automatically awakened by the system after a certain period of time: Thread.sleep() method; Object.wait() with the Timeout parameter set Method; Thread.join() method with Thimeout parameter set; LockSupport.parkNanos() method; LockSuport.parkUnitl() method

Blocked Waiting to acquire an exclusive lock

End Terminated The thread status of the terminated thread


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn