preface

The use of threads is particularly important in Java concurrent programming. Understanding the origin of threads, usage scenarios, and considerations is a necessary skill to be a qualified Java programmer. In this article, the origin of threads, the difference between processes and threads, and the use of threads are briefly introduced. Hope that through this article, partners can have a deeper understanding of threads.

Learn about threads from operating system development

The emergence of thread, inseparable from the process. The emergence of process and inseparable from the operating system. The development of operating system promotes the rise of thread and process technology. So understanding the evolution of operating systems is particularly important to our understanding of threads. The entire operating system is roughly divided into the following stages:

  • Manual operation
  • Single channel batch system
  • Multichannel batch system
  • Time-sharing operating system
  • Etc etc.

In the first three stages, the understanding of processes and threads is particularly important, so this article will focus on the first three stages.

Manual operation

The earliest computers did not appear in the true sense of the operating system, then the computer intelligence to solve simple mathematical problems, such as Jeong-hyun, Yu Hyun, etc. It also operates in a very simple way. The programmer loads the punched tape (or card) corresponding to the program and data into the input machine, then starts the input machine to enter the program and data into the computer’s memory, and then starts the program against the data through the console switch. After the calculation, the output machine outputs the calculation results; The user takes the results and removes the tape (or card) before letting the next user on. To take a simple example, suppose we need to send three commands to a computer: eat, shower, and sleep. In a traditional computer, we would get the following:

From the figure above, it is clear that the manual approach severely compromises system utilization. The computer remained idle while waiting for the user to input instructions.

Single channel batch system

In order to get rid of the time-consuming manual operation, the automatic transition of operations (procedures, data, instructions) is realized. Then came the single-channel batch system. Single-channel batch processing system in the original manual operation is the main difference between the reader and the host adds a tape storage equipment (disc) (below, the red dotted line), and matched to monitor on the host system, the specific operation mode is usually a batch job to input to the tape, and then to a tape by supervision program into memory the first homework, And gives operational control to the job. When the job is finished, control is returned to the supervisor, who then calls the second job on the tape (disk) into memory. The computer system then automatically proceeds job by job until all the jobs on the tape are complete. Taking the above instructions of eating, bathing and sleeping as examples, we can get the following picture:

It is important to note that although the single-channel batch operation system to solve the need when manual operation manual switching operation lead to the problem of low system utilization, but also because of the single channel batch system was added memory will work one by one, then a homework because waiting for tape (plate) or other I/O operation was suspended, the computer can only have been blocked, Until the I/O is complete. For CPU-intensive programs, there are relatively few I/O operations and therefore very little time wasted. However, CPU resources are wasted in scenarios with a large number of I/O operations.

Multichannel batch system

In order to solve the single-channel batch system because of input/output (I/O) requests, resulting in the computer waiting for I/O to complete the waste of computer resources. Next came multichannel batch systems. The main difference between a multichannel batch system and a single-channel batch system is that it allows one or more jobs in memory. When one job is waiting for I/O processing, the multichannel batch system schedules another job for the computer to execute through the corresponding scheduling algorithm. Thus, the UTILIZATION rate of CPU is improved. As shown below:

Origin of process

A very important pattern developed in multi-channel batch systems is to allow multiple jobs to enter memory and run. Since there are multiple jobs stored in memory, how can multiple jobs be distinguished? When a job is suspended waiting for I/O, how can it be restored to its previous running state?

So at this time, smart people invented the concept of process, using process to store the data and running state of each job, at the same time, each process is divided into the corresponding memory address space (code, data, process space, open files), and requires that the process can only use its own memory space. Then the differentiation and recovery of the job can be achieved.

Origin of threads

The concept of processes introduced in multichannel batch processing systems does improve the efficiency of computer operation, but the disadvantages of using only processes to handle the concurrency of programs become more and more prominent, because a process can only do one thing at a time. If a program has multiple tasks, they can only be executed one by one.

Concurrency: At a macro level, it appears that multiple tasks are executing in the computer at the same time.

Take eating as an example. Suppose there are two tasks in the eating process, one is to watch TV and one is to eat. Now we want computers to eat and watch TV at the same time. So according to the rules of multi-channel batch processing system, the actual computer scheduling is based on the process as the unit of scheduling, so we want to eat while watching TV behavior, can only be split into two processes. But we know that processes store a lot of information (data, process health information, etc.). So when the computer process switch, there must be a lot of time and space consumption (because each process corresponds to different memory address space, process switch, is actually the processor to deal with different address space). If we don’t split it into two processes and eat in the same process, then the two tasks must be executed in sequence, which again defeats the purpose of eating while watching TV.

So the question is, can we switch tasks in the process without switching memory address space? Clever people took the idea of threads one step further. Different tasks in the process are represented by threads, and the unit of actual computer scheduling is transferred to threads. This avoids switching the memory address space of the process and achieves concurrent execution of multiple tasks. The details are shown in the figure below:

The difference between processes and threads

In front of the explanation of the origin of process and thread, there may be a bit of confusion, let us sum up the relationship between process and thread.

  • Processes are the unit of computer resource allocation, and threads are the basic unit of computer scheduling.
  • A process consists of one or more threads. Threads represent different tasks in a process.
  • Processes are independent of each other. Threads in the same process share the program’s memory space (including code segments, data sets, heaps, etc.) and some process-level resources (such as open files and signals).
  • Process switching is caused by space-time overhead.

The relationship between processes and threads is shown below:

Usage scenarios for threads

The appearance of threads does improve the performance of programs, so what are the usage scenarios of threads?

  • Perform background tasks. In many scenarios, scheduled batch tasks may be performed, such as sending SMS messages and generating batch files. In these scenarios, it can be done through multiple threads.
  • Asynchronous processing, such as sending coupons or SMS messages to users after successful registration, can be carried out in an asynchronous way, on the one hand, improve the performance of the main program; On the other hand, it can decouple core functions and prevent non-core functions from affecting core functions.
  • Distributed processing, such as fork/join, splits a task into subtasks to execute separately.
  • Thread task distribution in the BIO model is also a common usage scenario, one request per thread.

Precautions for thread usage

Although we can create multiple threads in the program to improve the efficiency and throughput of the program, the following problems will also occur:

  • Multiple threads share the resources and address space of the process they belong to. If multiple threads want to access a resource at the same time, what should I do?
  • Since threads can improve performance, is it better to create as many threads as possible in your program?

These issues are really about thread synchronization and creating a reasonable number of threads. If you are interested in this part, you can pay attention to subsequent articles or refer to relevant materials.

The last

Standing on the shoulders of giants, can see further ~

  • The history of threads
  • Concurrent programming in Java: The Origin of processes and threads