What is a thread? What is the difference between it and the process? Why multithreading ?, Multi-thread

Source: Internet
Author: User

What is a thread? What is the difference between it and the process? Why multithreading ?, Multi-thread

A thread is an execution unit in which the program code can be executed during execution. In Java, a thread has four states: running, ready, suspended, and ended.

A process is a program that is being executed. A thread, also known as a lightweight process, is the smallest unit of program execution. A process can have multiple threads, and each thread shares the program's memory space (code segment, data segment, and heap space) and some process-level resources (such as open files), but each thread has its own stack space, as shown in the relationship between processes and threads:


At the operating system level, program execution is based on processes, and each process usually has multiple threads for concurrent execution without affecting each other. Why is multithreading required? In fact, the use of multithreading has brought great convenience to program development. Specifically, there are the following aspects:

1) multithreading can reduce the response time of the program. When a single thread (a single thread refers to a sequence of valid operations in the program execution process, and there is a clear execution sequence between different operations, if an operation is time-consuming or in a long wait (such as waiting for a network response), the program will not respond to operations such as the mouse and keyboard. After multithreading, this time-consuming thread can be allocated to a separate thread for execution, so that the program has better interaction.

2) compared with processes, the thread creation and switching overhead is smaller. Since an independent address space must be allocated to the thread to start a new thread, many data structures are established to maintain information such as thread code segments and data segments, the threads running in the same process share code segments and data segments, and the thread startup or switching overhead is much less than the process. At the same time, multithreading is highly efficient in data sharing.

3) multi-CPU or multi-core computers have the ability to execute multiple threads. If a single thread is used, computer resources cannot be reused, resulting in a huge waste of resources. Therefore, using multithreading on multiple CPU computers can improve the CPU utilization.

4) multithreading can simplify the program structure and make the program easy to understand and maintain. A very complex process can be divided into multiple threads for execution.


From: new book "Java programmer interview test book" Official Website


What is the difference between a process and a thread? What is multithreading?

What is a system process?
A process is an application running in the system. A thread is the basic unit for the system to allocate processor time resources, or a unit that is executed independently within the process. For the operating system, the scheduling unit is a thread. A process includes at least one thread, which is usually called as the main thread. A process starts from the execution of the main thread and creates one or more additional threads.
What is the difference between a process and a thread? A process is an instance of the execution program. For example, when you run the Notepad program (Nodepad), you create a process to hold the code that makes up Notepad.exe and call the dynamic link library. Each process runs in a dedicated and protected address space. Therefore, if you run two copies of notepad at the same time, the data used by the program is independent of each other in the respective instances. The data opened by the second instance of the program cannot be seen in a copy of notepad.
The sandbox is used as an example to describe. A process is like a sandbox. The thread is like a child in the sandbox. The children ran around in the sandbox, and may drag the sand into the eyes of other children. They would kick each other or bite each other. However, the difference between these sandboxes is that each sandbox is completely closed by walls and ceilings. No matter how hard the children in the sandbox are, they will not affect other children in other sandboxes. Therefore, each process is like a protected sandbox. No one can go in or out without permission.
In fact, the thread runs but the process does not run. The only way for two processes to obtain dedicated data or memory is to share the memory block through the Protocol. This is a collaboration policy. Next let's analyze the process tab in the task manager.
The process here refers to a series of processes that are identified by the executable program instances they run. This is why the ing name is given in the first column of the Process tab. Note that there is no process name column. A process does not have a ing name independent of the instance to which it belongs. In other words, if you run five Beibei, you will see five processes called notepad.exe. How are they different from each other? One way is through their process ID, because each process has its unique encoding. The process ID is generated by Windows NT or Windows 2000 and can be used cyclically. Therefore, process IDs will not be larger and larger, and they can be recycled. The third column is the percentage of CPU time occupied by the threads in the process. It is not the number of the CPU, but the percentage of CPU time occupied by the process. At this time, my system is basically idle. Although the system seems to use only a small amount of CPU time every second or so, the idle process of the system still consumes about 99% of the CPU time.
The fourth column shows the CPU time, which is the hour, minute, And seconds occupied by threads in the process. Please note that I use the word "Occupy" for threads in the process. This does not necessarily mean that it is the total CPU time consumed by the process, because, as we will see later, the NT timing method is that when a specific clock interval is triggered, no matter who happens to be in the current thread, it will be calculated within the CPU cycle. Generally, the clock runs at 10 milliseconds in most NT systems. The heart beats every 10 ms of NT. Some driver code snippets run and show who is the current thread. Let's keep the last 10 milliseconds of CPU time in its account. Therefore, if a thread starts to run and is finished after 8 ms of continuous running, then the second thread starts to run and lasts for 2 ms, the clock is triggered, please guess which thread the entire 10 ms clock cycle is recorded in? The answer is the second thread. Therefore, there are some inherent inaccuracies in NT, and NT timing is in this way. The actual situation is also true. In most 32-bit operating systems, there is an interval-Based Timing mechanism. Remember this, because sometimes when you observe the total CPU usage of a thread, it may appear although the thread may have been running for more than 100,000 times, however, the CPU usage may be zero or very short-lived, so the above explanation is the reason. The above is the basic information column we can see in the process tab of the task manager.

What is a thread?

What is a thread ?...... Remaining full text>

What is a thread? What is the difference between a thread and a process?

Processes and threads are the basic units for running programs embodied in the operating system. The system uses these basic units to achieve system concurrency for applications.
The difference between a process and a thread is:
In short, a program has at least one process, and a process has at least one thread.
The thread division scale is smaller than the process, making the multi-thread program highly concurrent.
In addition, the process has independent memory units during execution, and multiple threads share the memory, which greatly improves the program running efficiency.
The execution process of a thread is different from that of a process. Each Independent thread has a program running entry, sequence execution sequence, and program exit. But the thread cannot be executed independently. It must exist in the application and the application provides multiple thread execution control.
Logically, multithreading means that multiple execution parts in an application can be executed simultaneously. However, the operating system does not view multiple threads as multiple independent applications to implement process scheduling, management, and resource allocation. This is an important difference between processes and threads.

A process is a running activity of a program with certain independent functions. A process is an independent unit for the system to allocate and schedule resources. A thread is an entity of a process. It is the basic unit for CPU scheduling and scheduling. It is smaller than a process and can run independently. the thread itself basically does not have system resources, and only has a few resources (such as program counters, a set of registers and stacks) that are essential for running ), however, it can share all resources of a process with other threads of the same process.
One thread can create and cancel another thread, and multiple threads in the same process can be concurrently executed.

I summarized it during last semester's course review.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.