In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you what are the differences between processes and threads in java. I hope you will gain something after reading this article. Let's discuss it together.
Preface
Process and thread are the basic concepts of the operating system, but they are abstract and not easy to master.
Recently, I read an article and found that there is a good analogy that can explain them clearly and easily.
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.
It is not difficult to see that mutex is a special case of semaphore. In other words, the latter can be used instead of the former. However, because mutex is simple and efficient, this design is adopted when it is necessary to ensure the exclusive use of resources.
Welcome to follow my public Hao [programmer chase wind], the article will be updated in it, the collated information will also be put in it.
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.
After reading this article, I believe you have a certain understanding of "what is the difference between processes and threads in java". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!
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.