This article brings you a summary and analysis of multi-threading knowledge in Java. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Process: A running program is an independent unit for resource allocation and calling by the system.
A process is a dynamic execution process of a program on a data set.
The process generally consists of three parts: program, data set, and process control block.
Each process has its own memory space and system resources.
The program we write is used to describe what functions the process wants to complete and how to complete it;
The data set is what the program does during its execution. Resources to be used;
The process control block is used to record the external characteristics of the process and describe the execution change process of the process. The system can use it to control and manage the process. It is a system-aware process. The unique identifier exists.
Example the process:
(1) Imagine a computer scientist with good cooking skills is baking a birthday cake for his daughter ;
(2) He has a recipe for making a birthday cake and has the necessary ingredients in the kitchen: flour, eggs, sugar, etc.
(3) In this metaphor, the recipe for making a cake is the program;
(4) The computer scientist is the processor cpu;
(5) The various raw materials for making the cake are the input data.
(6) The process is the sum of a series of actions such as the chef reading the recipe, getting various ingredients, and baking the cake.
(7) Now suppose that the computer scientist’s son comes out crying and says that his head was stung by a bee;
(8) The computer scientist records what he did according to the recipe, and It is to save the current state of the process;
(9) Then take out a first aid manual and follow the instructions to deal with the sting;
(10) At this time we will see the processor switching from one process to another High-priority processes;
(11) Each process has its own program (recipes and first aid manuals);
(12) After the bee sting is treated, the computer scientist comes back to make cakes;
(13) Continue from where he left off.
The emergence of threads is to reduce the consumption of context switching and improve the concurrency of the system.
Threads break through the defect that a process can only do one thing, making intra-process concurrency possible.
Example thread:
(1) Suppose a text program needs to receive keyboard input, display the content on the screen, and save the information to the hard disk.
(2) If there is only one process, it will inevitably cause the embarrassment of being able to do only one thing at the same time, that is, when saving, no keyboard input is possible;
(3) If there are multiple processes, each process is responsible for one Task;
(4) Process A is responsible for keyboard input, process B is responsible for displaying content on the screen, and process C is responsible for saving content to the hard disk;
(5) The collaboration between A, B, and C here involves Process communication issues, and they all have the same content: text content;
(6) Constant switching will cause performance losses.
(7) If there is a mechanism that allows A, B, and C to share resources;
(8) In this way, less content needs to be saved and restored for context switching;
(9) At the same time, It can reduce the performance loss caused by communication.
(10) This mechanism is thread.
Threads are also called lightweight processes.
It is a basic CPU execution unit and the smallest unit in the program execution process.
Composed of thread id, program counter, register set and stack
The introduction of threads reduces the overhead of concurrent execution of programs and improves the concurrency performance of the operating system.
Threads do not have their own system resources.
A process is a running activity of a program in the computer on a certain data collection.
The process is the basic unit of resource allocation and scheduling in the system and the basis of the operating system structure
The thread is an entity of the process and is The basic unit of CPU scheduling and dispatch
Threads are smaller than processes and can run independently
The relationship between processes and threads:
(1) A thread can only belong to one process, and a process can have multiple threads, but there is at least one thread
(2) Resources are allocated to processes, and the same process All threads share all resources of the process
(3) The cpu is allocated to the thread, that is, the thread is actually running on the cpu
(1) Because the garbage collection thread must also be started when the jvm is started, otherwise memory overflow will easily occur;
(2) The current garbage collection thread plus the previous main thread , at least two threads are started, so the startup of jvm is multi-threaded.
Have a good start: the status of the thread
Inner Strength Method: Methods (mechanisms) that each object has
Taizu Changquan: Basic thread class
Nine Yin Manual: Advanced Multi-thread control class
New: New state, when the thread object is created , that is, entering the new state, such as: Thread t = new MyThread()
Runnable: ready state, when the start() method (t.start()) of the thread object is called, The thread enters the ready state. The thread in the ready state only means that the thread is ready and waiting for CPU scheduling execution at any time. It does not mean that the thread will execute immediately after executing t.start()
Running: Running state. When the CPU starts scheduling threads in the ready state, the thread can actually be executed at this time, that is, it enters the running state.
Blocked: Blocked state. The thread in the running state temporarily gives up the right to use the CPU for some reason, stops execution, and enters the blocked state until it enters the ready state. state, there is a chance to be called by the CPU again to enter the running state
Dead: Death state, the thread has finished executing or exited the run() method due to an exception, and the thread ends its life cycle
Note:
(1) The ready state is the only entrance to the running state
(2) If a thread wants to enter the running state for execution, it must first be in In the ready state
(3) According to the cause of the blocking, the blocking state can be divided into three types:
[1] Waiting for blocking: The thread in the running state executes the wait() method, so that the thread enters the waiting state. Blocking state
[2] Synchronous blocking: When the thread fails to acquire the synchronized synchronization lock (because the lock is occupied by other threads), it will enter the synchronized blocking state
[3] Other blocking: By calling the thread's sleep() or When join() or an I/O request is issued, the thread will enter the blocking state. When the sleep() state times out, join() waits for the thread to terminate or times out, or the I/O processing is completed, the thread re-enters the ready state. Method: Each object has methods
synchronized, wait, and notify are synchronization tools that any object has
(3) The monitor does not play a role during non-multithreaded coding. On the contrary, if it is within the synchronized range, the monitor plays a role.
wait/notify: Both must exist in the synchronized block
And these three keywords target the same monitor, which means that after wait, other threads can enter the synchronized block for execution
Taizu Changquan: Basic Thread Class
Thread class
The above is the detailed content of Summary and analysis of multi-threading knowledge in java. For more information, please follow other related articles on the PHP Chinese website!