Thread Detail Concepts

Source: Internet
Author: User

I. Overview
In Windows, each open running application or daemon, such as running QQ, Google Browser, NetEase Cloud music, resource manager, etc. are a process. We feel that these programs are running "at the same time", but in fact, a processor can only run a process at the same time, but the CPU in high-speed rotation let us have the illusion that we do not feel the interruption because the CPU execution speed relative to our feeling is too fast.

We learn c, write Java, and most of the time we do it in single-threaded programming. There is only one main line that executes sequentially--the program starts with the main method, executes the code sequentially, until the full part code is executed, unless a line of code error causes the main line to block.

Multithreading is the execution of multiple sequential streams "at the same time" and they do not interfere with each other. Multi-threaded application scenario, to give a very simple example, we write a Web site backstage, if only a single thread, then only one user access to the site, this reality? Should we create a thread for each user so that each user can "simultaneously" access the site.

Here, all of my "at the same time" are quoted, because in the computer field, the macro "at the same time" and the Micro "at the same time" is different, they are called concurrency and parallelism.

Concurrency means that only one instruction (or one process, one thread) can run at the same time, but because the CPU rotates faster than expected, on the macro level, there are multiple instructions to execute simultaneously.
and parallel, as we usually understand, is that at the same moment there are multiple instructions executed simultaneously on multiple processing machines.

Ii. details of the process

1. Concept
We all know that the program, a program is static, is usually stored in the external memory. And when the program is transferred into memory, it becomes a process.
As the name implies, a process is an ongoing program, and it is a dynamic concept. System is the basic unit of resource allocation and dispatch.

2, the process generally has the following three characteristics:
A, independence: each process has its own independent resources, has a private address space, its size and the number of processor bits, such as the WIN32 system, 32-bit address mapping 4GB address space, where the low address of 2GB as the user mode of the virtual address space (application can be shared, between threads Independent), High address 2GB as the virtual address space of the kernel mode (System use).
B, dynamic: From the concept of the process, it can be seen that the process is a running program. The process has the concept of time, status (blog), life cycle and other dynamic concepts.
C, Concurrency: Multiple processes execute concurrently on a single processor.

3, the process mainly contains three parts:
A, program code: Used to describe the process to complete the function.
B, data collection: The data and work area required for program execution.
C, the PCB control block: Contains the process of descriptive information and controls information, is the only sign of the process, and precisely because of the PCB, the process has become a dynamic concept.

Third, threading and multithreading details

1, the concept, by the thread and process relationship discussion

Within a process, there may be many sequential execution flows, and each sequential execution flow is a thread.
Most operating systems now use preemptive multitasking to support multi-process concurrency, and multithreading is a multi-process extension that enables a process to handle multiple tasks concurrently, like a processor, and threads are the basic unit of concurrent execution in a process, and threads are thus referred to as "lightweight processes." A process can contain multiple threads, and each thread has its parent process.

2. Resources for Threads

We may have known about the memory mechanism of Java, we have heard of the term "thread private", "Thread sharing", the method stack (which holds local variables through the local variables table in the stack frame), the program counters are thread-private, their memory space is dedicated to a thread, and the parent process's resources (such as code snippets, Process public data) is shared among child threads and is carefully programmed for shared resource areas to ensure that no other threads are affected.

3, multithreading advantages, from the thread and the process of the difference between the discussion

A, a separate thread is less isolated than a delimited process because threads share the resources of a process and are easier to communicate between threads.
b, the system to create a process to allocate resources for the process, and create a thread, only need to allocate thread-private resources, the cost is much smaller, multi-threaded instead of multiple processes to achieve high concurrency and higher efficiency.
C, multi-threaded programming more convenient, program code level can be achieved.

Thread Detail Concepts

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.