Home > Java > javaTutorial > What are the basic knowledge of Java's underlying operating system and concurrency?

What are the basic knowledge of Java's underlying operating system and concurrency?

WBOY
Release: 2023-04-29 21:25:06
forward
832 people have browsed it

1. Modern computer hardware structure

What are the basic knowledge of Javas underlying operating system and concurrency?

##Core part: CPU, memory

1.CPU internal structure

What are the basic knowledge of Javas underlying operating system and concurrency?

  • Control unit: The command and control center of the entire CPU

  • ##Computing unit :

    Calculator core, performs arithmetic operations and logical operations. The arithmetic unit receives instructions from the control unit and performs actions

  • Storage unit:

    The place where data is temporarily stored in the CPU, including CPU on-chip cache Cache and Register group

    #1.1.CPU cache structure
In order to improve execution efficiency, modern CPUs reduce the interaction between the CPU and memory (interaction affects the CPU efficiency), generally a multi-level cache architecture is integrated on the CPU. The common three-level cache structure

L1 Cache is divided into data cache and instruction cache, and is exclusive to the logical core
  • L2 Cache, exclusive to physical cores, shared by logical cores
  • L3 Cache, shared by all physical cores

The three-level cache architecture of this machine is as follows: L1 Cache is divided into two types, instruction storage unit (storage instructions), and logical storage unit (storage logic). Theoretically, a machine can have multiple CPUs, determined by the slots. A CPU can have multiple cores, and a core can have multiple logical processors. What are the basic knowledge of Javas underlying operating system and concurrency?

RegisterWhat are the basic knowledge of Javas underlying operating system and concurrency? is an internal component of the CPU and has very fast read and write speeds.

The CPU will only read data from the register. Each CPU has a unique register that cannot be accessed by other CPUs.

Using registers can reduce the number of times the CPU accesses memory, thereby increasing the CPU's working speed. The closer to the CPU, the faster the reading speed

. According to Moore's Law, the CPU is developing at a rate of doubling every 18 months, while the development speed of memory and hard disk is much faster. Far from keeping up. In order to

solve the problem of mismatch between CPU computing speed and I\O speed, the CPU began to have a small amount of built-in cache Lx Cache (CPU space is limited and storage element size is limited).

    Memory storage space size: Memory > L3 Cache > L2 Cache > L1 Cache > Register
  • Memory read speed: Register > L1 Cache > L2 Cache > L3 Cache > Memory
  • The cache is composed of the smallest storage Block ---
  • CacheLine
  • consists of blocks. The cache line size is usually 64byte. The cache size of my machine L1 is 512K, which is composed of 512 * 1024/64 cache lines.

CPU reading memory data process:

CPU can only obtain data directly from registers. Assuming that data x = 0 is in memory, its value acquisition process is as follows: Determine whether it exists in the register

If it does not exist, traverse the L1 Cache to see if it exists. There is no traversal of the L2 Cache, there is no traversal of the L2 Cache, and there is no traversal of the L3 Cache. If an intermediate process exists, the Cache line will be locked and copied to the upper level until it reaches the register.

If it is not found in the Cache, it will be searched in the memory. It will first notify the memory controller to occupy the bus bandwidth, notify the memory to lock, initiate a memory read request, wait for the response, and copy the response data to the L3 Cache. Note: The entire process is locked until the CPU is unlocked.

Locality principle: When the CPU accesses the storage device, whether it is accessing data or accessing instructions, they tend to gather in a continuous in the area.

There are two types of locality principles:

    Temporal Locality:
  • If a The information item is being accessed, so it is likely to be accessed again in the near future. For example, loops, recursion, repeated calls of methods, etc.

  • Spatial Locality:
  • If a memory location is referenced, then its nearby locations will also be referenced in the future. For example, sequentially executed code, two objects created continuously, arrays, etc.

  • Example of spatial locality:
For a large two-dimensional array, cumulative summation row by row will be much faster than column-by-column summation. When the CPU reads data in the memory, all the attached data will be read in.

1.2.CPU operating security level

The CPU is divided into 4 operating levels:

    ring0
  • Kernel state

  • ring1
  • ring2

  • ring3 User Mode

Linux and Windows only use two levels: ring0, ring3. Internal program instructions within the operating system usually run at the ring0 level. Third parties outside the operating system The program runs at the ring3 level. If a third-party program wants to call the internal functions of the operating system, because the operation security level is not enough, it must switch the CPU running state from ring3 to ring0, and then execute the system function and create a thread. Thread blocking and waking up are heavy operations. , because the CPU needs to switch operating states.

JVM thread creation is a CPU process:

  • Step 1: CPU switches from ring3 to ring0 to create a thread

  • The second step: After the creation is completed, the CPU switches from ring0 back to ring3

  • The third step: The thread executes the JVM program

  • Step 4: After the thread is executed, destroy it and switch back to ring0

  • Step 5: Destroy the thread and switch back to ring3

2. Operating system memory management

In order to ensure safe isolation and stability of program operation, the operating system has two concepts: User space and Kernel space. Take the 4G memory space of a 32-bit operating system as an example:

What are the basic knowledge of Javas underlying operating system and concurrency?

Linux reserves several page frames for kernel code and data structures, and these pages will never be transferred. Out to disk (4GB memory space, 3GB available for user programs). As shown in the figure, the linear address in the green part can be referenced by user code and kernel code ( is user space ). The linear address in the yellow part can only be accessed by the kernel code ( is the kernel space ).

Processes and threads can only run in usermode(usermode) or kernelmode(kernelmode). User programs run in user mode, while system calls run in kernel mode.

Use a general stack (user space stack) in user mode, use a fixed size stack (kernel space stack, generally the size of a memory page) in kernel mode, that is, each process and thread actually There are two stacks, running in user mode and kernel mode respectively.

The basic unit thread of CPU scheduling is also divided into:

  • ##Kernel thread model (KLT): Used by Java , the kernel saves the status and context information of the thread, and thread blocking will not cause process blocking. On multiprocessor systems, multiple threads run in parallel on multiple processors. The creation, scheduling and management of threads are completed by the kernel, and the efficiency is slower than ULT and faster than process operations.

  • User thread model (ULT): Does not rely on the operating system core. The application provides functions to create, synchronize, schedule and manage threads to control user threads. No user mode/kernel mode switching is required, and the speed is fast. The kernel has no awareness of ULT. When a thread blocks, the process (including all its threads) blocks

What are the basic knowledge of Javas underlying operating system and concurrency?

Threads have two stacks, one in user space , one in kernel space. Blocking, creating, and killing threads will abandon the user space stack and transfer it to the kernel space, and then transfer it to the user space after the execution is completed.

3. Processes and threads

Process: The smallest unit of operating system resource allocation, for example: start a Java program, the operating system will Create a Java process, which can contain multiple threads.

Thread: The smallest unit of the operating system scheduling CPU. Threads have their own attributes such as counters, stacks, and local variables, and can access shared memory variables. The CPU switches between these threads at high speed, allowing users to feel that these threads are executing at the same time (concurrency).

Thread switching up and down: Save the intermediate state of the previous thread running and execute the next thread

What are the basic knowledge of Javas underlying operating system and concurrency?

  • Serial: There is no overlap in time. The previous task is not completed, and the next task can only wait.

  • Parallel: There is overlap in time. , two tasks are executed at the same time without interfering with each other

  • Concurrency: Running two tasks interferes with each other. At the same time point, only one task is executed. Alternate execution

The above is the detailed content of What are the basic knowledge of Java's underlying operating system and concurrency?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template