In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the knowledge of "the relationship between Java processes, threads, parallelism and concurrency". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Processes and threads
Process refers to a dynamic execution process of a program. Usually we say that the program being executed in the computer is a process, and each program corresponds to a process. A process contains a complete process from code loading to execution, which is the smallest unit of operating system resource allocation.
A thread is a smaller execution unit than a process and is the basic unit of CPU scheduling and dispatching. Each process has at least one thread, in turn, a thread can only belong to one process, and the thread can schedule and operate all the resources of the process. Threads can be scheduled not only by the operating system kernel, but also by user programs.
Concurrency and parallelism
Both concurrency and parallelism can be relative to processes or threads. Concurrency refers to the multiplexing of multiple processes or threads by one or more CPU. In simple language, CPU executes multiple tasks in turn, and each task is executed for a short period of time. From a macro point of view, it looks as if all tasks are being executed at the same time. Parallelism means that multiple processes or threads are executed at the same time, which is really executed at the same time, and it must be supported by multiple CPU. The following figure is a parallel and parallel execution time diagram. For concurrency, the thread executes for a period of time, then thread 2 executes for a period of time, and thread 3 executes for another period of time. Each thread takes turns to get the execution time of the CPU, in which case only one CPU is needed. For parallelism, thread one, thread two, and thread three execute at the same time, in which case three CPU are required to implement.
Phellodendron mandshurica (Thunb.)
For Java concurrency, it is implemented on the Java platform to implement the concurrency mechanism. Threads and thread concurrency are provided on the Java platform.
Multithreading can improve execution efficiency
We learned earlier that multithreading can achieve concurrent and parallel execution, so multithreading can improve overall efficiency. If multithreading is not supported, when an executing task enters a waiting blocking state, it may be inefficient because of blocking. In figure 1 below, a request task starts to wait for a response after initiating a request. At this time, the thread occupies the CPU and does not work. From the entire runtime, you can see that there is very little time to actually run (green square), which is inefficient. However, if multithreading concurrency is supported, you can do other work while waiting for blocking. In addition, in a multi-CPU environment, if a task can be divided into multiple small tasks, it can be executed with multiple CPU at the same time, so that the task can be completed more quickly. After all, the running capacity of a single CPU is limited. As shown in figure 2 below, once the task is divided into three small tasks, it can be executed in parallel in a multi-CPU environment, which greatly reduces the overall execution time.
Single thread blocking state
Multithreading can improve user experience
Multithreading can also improve the user experience, which can lead to a poor user experience if a thread's tasks include both time-consuming and user-interactive tasks. As shown in the picture below, is it very uncomfortable if you see these windows spinning all the time and unable to operate them? A thread initiates a request and starts waiting for the result of the request, while the user interface remains stuck and unresponsive. Through multithreading, we can divide the task into two parts: the request task and the interface operation, so that we can maintain the response to the interface operation after the request, so as to provide a better user experience.
Multithreading makes coding more difficult
There is no such thing as a free lunch, and multithreading comes at a price. From a coding point of view, multithreading makes coding more complex, essentially because of the multithreading mechanism and modern computer structure. Even with the efforts of programming language design experts, there are many languages and models that simplify multithreaded programming, but multithreading programming is still much more complicated than single-threading. There are several layers of caches and registers in the middle of data storage from main storage to CPU, and multiple threads may access shared memory, which involves the problem of data synchronization, which increases the complexity of multithread programming. In addition, communication between threads is also troublesome, which increases the complexity of multithreaded coding. In short, although many programming languages try to provide us with more convenient multithreaded programming, it is still impossible to completely shield the complexity of multithreading and computer structure at the language level. so no matter what language we use, we need to think more about multithreaded coding.
Context switching and resource overhead
In addition to increasing the difficulty of coding, multithreading also brings actual loss in the process of execution, including resource overhead and context switching overhead. The resource overhead mainly includes the memory resources occupied by itself, the thread local stack overhead at execution time, and the overhead of managing these threads. The cost of context switching is because CPU needs to do on-site protection and recovery work when switching from one thread to another, including thread identification, register memory, thread state, thread priority, thread resource list and so on. As shown in the following figure, assume that threads 1 and 2 are executed by some CPU. After executing for a period of time, the thread saves the relevant information in the field data structure, while the thread data structure is stored in the main storage, and then recovers the thread 2 related information from the field data structure corresponding to thread 2. Thread 2 begins to execute after completing the on-site recovery. The next process, in turn, switches from thread two to thread one.
In practice, we should comprehensively consider the advantages and disadvantages of multithreading, can not blindly pursue multithreading, before using multithreading, we must measure the benefits and costs of multithreading. Phellodendron mandshurica (Thunb.)
This is the end of "the relationship between Java processes, threads, parallelism and concurrency". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.