In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail what processes and threads are in high concurrency in java. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
Process
Process is a running activity of a program in a computer on a certain data set, the basic unit of resource allocation and scheduling of the system, and the basis of the structure of the operating system. A program is a description of instructions, data and its organizational form, and a process is the entity of a program.
The characteristics of a process:
Dynamic: a process is an execution process of a program, which is temporary, has a lifetime, is dynamically generated, and dynamically dies out.
Concurrency: any process can be executed concurrently with others
Independence: a process is an independent unit for resource allocation and scheduling in the system.
Structure: process consists of three parts: program, data and process control block.
We often use the windows system, often see the .exe suffix file, double-click the .exe file, the instructions in this file will be loaded by the system, then we can get a process about the .exe program. The process is "alive", or is being executed.
Open the task manager in window, and you can see the running processes on the current system, as shown below:
Thread
Threads are lightweight processes and the smallest unit of program execution. Multi-threads instead of multi-processes are used to design concurrent programs because the cost of switching and scheduling between threads is far less than that of processes.
Let's take a look at the state diagram of the thread with a diagram:
All states of the thread are defined in the State enumeration in java.lang.Thread, such as:
Public enum State {NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, TERMINATED;}
An introduction to several states of the thread:
New: represents the thread that has just been created, which has not yet started execution
RUNNABLE: running state, the thread will be in this state after the start () method is called
BLOCKED: blocking state. When a thread encounters a synchronized synchronization block in the course of execution, but the synchronization block has been acquired by other threads and has not been released, the current thread will enter a blocking state and suspend execution until the lock is acquired. When the thread acquires the lock, it enters the running state (RUNNABLE)
WAITING: wait status. And TIME_WAITING both mean waiting state, the difference is that WAITING will enter an unlimited wait, while TIME_WAITING will enter a limited time waiting, so what on earth is the waiting thread waiting for? Generally speaking, the thread of WAITING is officially waiting for some special event. For example, the thread waiting through the wait () method is waiting for the notify () method, while the thread waiting through the join () method is waiting for the termination of the target thread. Once you wait for the expected event, the thread enters the RUNNABLE running state again.
TERMINATED: indicates the end state, and the thread enters the end state after execution.
Note: after starting from the NEW state, the thread cannot return to the NEW state. Similarly, the thread dealing with the TERMINATED state cannot return to the RUNNABLE state.
A simple explanation of processes and threads
Process and thread are the basic concepts of the operating system, but they are abstract and not easy to master.
1. The core of the computer is CPU, which undertakes all the computing tasks. It is like a factory, running all the time.
two。 It is assumed that the power of the factory is limited and can only be supplied to one workshop at a time. In other words, when one workshop starts, all other workshops must be shut down. The implication behind this is that a single CPU can only run one task at a time.
3. A process is like a workshop in a factory. It represents a single task that CPU can handle. At any given time, CPU is always running one process and other processes are not running.
4. There can be many workers in a workshop. They worked together to accomplish a task.
5. Threads are like workers in a workshop. A process can include multiple threads.
6. The space of the workshop is shared by workers, for example, many rooms are accessible to every worker. This symbolizes that the memory space of a process is shared, and each thread can use the shared memory.
7. However, the size of each room is different, and some rooms can only hold up to one person, such as a toilet. When there's someone inside, no one else can go in. This means that when one thread uses some shared memory, other threads must wait for it to finish before it can be used.
8. A simple way to prevent others from entering is to add a lock to the door. Those who arrive first lock the door, and when those who arrive later see the lock, they line up at the door and wait for the lock to open before entering. This is called Mutual exclusion (Mutex), which prevents multiple threads from reading and writing to a certain area of memory at the same time.
9. There are also some rooms that can accommodate n people at the same time, such as the kitchen. In other words, if the number of people is greater than n, the extra people can only wait outside. This is like some areas of memory that can only be used by a fixed number of threads.
10. The solution at this time is to hang n keys at the door. The person who goes in will take a key and hang it back when he comes out. Those who arrived later found that the key was empty and knew that they had to wait in line at the door. This practice is called "Semaphore" and is used to ensure that multiple threads do not conflict with each other.
11. The design of the operating system can be summed up in three points:
(1) allow multiple tasks to run at the same time in the form of multiple processes
(2) allow a single task to run in different parts in the form of multithreading
(3) provide a coordination mechanism to prevent conflicts between processes and threads on the one hand, and allow resources to be shared between processes and threads on the other.
This is the end of this article on "what are processes and threads in high concurrency in java". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.