Java Interview Question 3: The difference and connection between process and thread

      When we use the computer to open QQ, then the computer starts a process. When we check the messages sent in QQ or scan various operations, we actually start the thread.

1. Process:

      A process is an application program running in the system. Once a program runs, it is a process, and a process is an independent unit of resource allocation and scheduling.

      A program is an ordered collection of a set of instructions, which has no meaning in itself, but a static entity. The process is different, it is the execution of the program on a certain data set. A process is a dynamic entity with its own life cycle. A process is created due to its creation, runs due to scheduling, is in a waiting state due to waiting for resources or time, and is cancelled due to completion of a task. It reflects the entire dynamic process of a program running on a certain data set. A program is a description of instructions, data and its organization, and a process is the entity of the program.

2. Thread:

       With the development of computers, the requirements for the CPU are getting higher and higher, and the switching between processes is expensive, and it can no longer meet the requirements of more and more complex programs. So there is a thread. A thread is a single sequential control flow in program execution. It is the smallest unit of program execution flow and the smallest unit of processor scheduling and allocation. It is included in the process and is the actual operation of the process. unit. A thread refers to a single sequential control flow in a process. There can be multiple concurrent threads in a process, and each thread executes different tasks in parallel. A thread is the smallest unit that an operating system can perform arithmetic scheduling. The thread itself does not own resources, but only the resources necessary for the thread to run, and all resources owned by the process are shared among threads in the same process.

3. The difference between process and thread:

    The main difference between process and thread lies in the different operating system resource management methods. A process has an independent address space. After a process crashes, it will not affect other modes in the protected mode, and a thread is just a different execution path in a process. Threads have their own stacks and execution variables, but there is no separate address space between threads, so the death of a thread means the death of the entire process. Therefore, a multi-process program is more robust than a multi-threaded program, but there is no separate address space between the threads. When switching, it consumes more resources and is less efficient. However, for operations that are performed simultaneously with some requirements and share certain variables, only threads can be used, not processes.

  1. Thread is the smallest unit of program execution, and process is the smallest unit of operating system allocation of resources;
  2. A program has at least one process, and a process is composed of one or more threads. A thread is the execution route of different codes of the code in a process;
  3. The processes are independent of each other, but each thread in the same process shares the memory space of the program (including code segments and data sets) and the resources in the same process. Threads cannot execute independently, and must be controlled by multiple threads in the application program.
  4. In an application, multiple parts of multiple threads can be executed in the same time,
  5. Scheduling and switching: fast thread context switching and process context switching

4. Reasons for using multithreading

    Because the thread is relatively cheap, the thread starts fast, exits faster, and the impact on system resources is relatively small. Most of the core object ownership is shared between threads.

5. Summary:

     Process and thread are both abstract concepts. Thread is an abstraction smaller than process. Both threads and processes can achieve concurrency. A process is the smallest unit that can own resources and run independently, as well as the smallest unit of process execution. Generally, a process will have multiple threads.

     The thread execution overhead is small, which is not conducive to the management and protection of resources; while the process is just the opposite, the thread overhead is large, which is convenient for the management and protection of resources.

Most operating systems adopt a preemptive scheduling method of time slice rotation. A task is executed for a short period of time to pause to execute the next task, and each task is executed in turn. A short period of time during the execution of a task is called a time slice. The state of the task being executed is called the running state. The suspended task is in the ready state, waiting for the arrival of the next time slice belonging to it. In this way, each task can be executed. Because the execution efficiency of the CPU is very high and the time slice is very short, the rapid switching between various tasks makes it feel like multiple tasks are being executed at the same time.

 

 

Guess you like

Origin blog.csdn.net/Sunshineoe/article/details/114681765