When we think of concurrent programming in Java, we usually think of processes, threads, parallelism, concurrency, and so on. So what do these concepts mean? What is the relationship between processes and threads? What is the relationship between concurrency and parallelism?

Processes and threads

Process refers to a dynamic execution process of the program, usually we say that the program being executed in the computer is a process, each program will correspond to a process. A process is the smallest unit of operating system resource allocation that contains the entire process from code loading to execution.

A thread is a smaller unit of execution than a process and is the basic unit of CPU scheduling and dispatch. Each process has at least one thread. Conversely, a thread can only belong to one process. The thread can schedule and calculate all the resources of the process. Threads can be controlled by either the operating system kernel or 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 cpus. In simple language, the CPU executes multiple tasks in turn, each of which executes for a short period of time, as if all the tasks are executing at the same time. Parallelism is when multiple processes or threads are executed at the same time. This is true simultaneous execution, and it must be supported by multiple cpus. The following diagram shows concurrent and parallel execution times. For concurrency, thread 1 executes for a period of time, then thread 2 executes for a period of time, then thread 3 executes for a period of time. Each thread gets CPU execution time in turn, and in this case only one CPU is needed. For parallelism, thread one, thread two, and thread three are executed simultaneously, in which case three cpus are required.

For Java concurrency, it is implemented on the Java platform to achieve the concurrency mechanism, the Java platform provides threads and thread concurrency

Multithreading improves execution efficiency

As we saw earlier, multithreading allows concurrent and parallel execution, so multithreading improves overall efficiency. If multithreading is not supported, the execution of a task in a waiting block state can be inefficient due to blocking. In Figure 1 below, a request task initiates a request and then waits for a response. At this time, the thread occupies the CPU and does not do any work. From the whole running line, you can see that the actual running time (green square) is very little, which is the operation inefficiency. However, if multithreaded concurrency is supported, you can do other work while waiting for a block. In addition, in a multi-CPU environment, if a task can be broken down into smaller tasks, it can be executed by multiple cpus at the same time, so that the task can be completed more quickly, since a single CPU has limited operating power. 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, greatly reducing the overall execution time.

Multithreading improves the user experience

Multithreading can also improve the user experience. If a thread’s tasks include both time-consuming tasks and user interaction tasks, it can lead to a poor user experience. In the picture below, if you see these Windows spinning all the time and you can’t operate them, isn’t it hard? A thread initiates a request and waits for the result of the request, and the user interface remains stuck. We can use multi-threading to divide tasks into request tasks and interface operations so that responses to interface operations can be maintained after the request to provide a better user experience.

Multithreading makes coding harder

There is no free lunch, and multithreading comes at a cost. From a coding standpoint, multithreading makes coding more complicated, essentially because of multithreading and modern computer architecture. Despite the efforts of programming language design experts, there are many languages and models that simplify multithreaded programming, but multithreaded programming is still much more complex than single-threaded programming. There are several layers of caches and registers between the primary storage and the CPU, and multiple threads may access the shared memory, which involves data synchronization issues and increases the complexity of multithreaded programming. In addition, the communication between threads is troublesome, which also increases the complexity of multithreaded coding. In short, although many programming languages try to provide us with more convenient multithreaded programming, the complexity of multithreading and computer structure still cannot be completely shielded at the language level, so no matter what language we use, we need to consider more for multithreaded coding.

Context switching and resource overhead

In addition to increasing coding difficulty, multithreading also introduces real costs during execution, including resource overhead and context-switching overhead. Resource overhead mainly includes the memory resources occupied by the thread itself, the overhead of the thread local stack at execution time, and the overhead of managing these threads. Context switching overhead is due to the need to do on-site protection and recovery of CPU switching from one thread to another, including thread identification, register memory, thread state, thread priority, thread resource inventory, and so on. As shown in the figure below, assume that thread one and thread two are executed by a CPU. Thread 1 saves the related information to the field data structure after executing for a period of time, and the thread data structure is stored in the main storage. Then, thread 2 recovers the related information from the field data structure corresponding to thread 2, and thread 2 starts executing after completing the field recovery. The next process is reversed, switching from thread two to thread one.

In practice, we should consider the advantages and disadvantages of multi-threading, not blindly to pursue multi-threading, we must measure the benefits and costs of multi-threading before using multi-threading.

More wonderful technology articles to share, welcome to pay attention to the public number: code agriculture framework