


What are the basic knowledge of Java's underlying operating system and concurrency?
1. Modern computer hardware structure
##Core part: CPU, memory
1.CPU internal structureControl 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
- 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.
Register is an internal component of the CPU and has very fast read and write speeds.
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 tosolve 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
CPU reading memory data process:- Memory read speed: Register > L1 Cache > L2 Cache > L3 Cache > Memory
- CacheLine
The cache is composed of the smallest storage Block ---
- 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 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.
- 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.
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:
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
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
-
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!

Start Spring using IntelliJIDEAUltimate version...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Java...

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6
Visual web development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.