Concurrent programming topics (a) - Concurrency and multithreading

1. Concurrent

1.1 Concurrent and Parallel

First, tell us about concurrency and parallelism, the two although only one word, but in fact it is essentially different, the concept is as follows:

  • Parallelism (Parallel) : means at the same time, a plurality of instructions executed on multiple processors simultaneously;
  • Concurrent (Concurrency) : refers to the moment, only one instruction execution, but more rapid rotation process instructions are executed so as to have the effect of simultaneous execution of multiple processes at the macro.

Concurrent programming topics (a) - Concurrency and multithreading

In the operating system, the installation of a plurality of programs concurrently refers to the period of time a plurality of programs are running on the macro, in which single-CPU system, each time only one program execution, i.e. the program microscopically is a time-alternating runs, just gives the impression that run at the same time, it is because the time-interleaved time is running very short.

In multiple CPU system, these programs can execute concurrently can be assigned to a plurality of processors (CPU), multi-parallel execution of tasks, i.e., with each processor to process a program can execute concurrently, so multiple programs can be executed simultaneously. He said the current computer market multicore CPU, multi-core processor is, the more cores, more parallel processing program, can greatly improve the efficiency of a computer operation.
Concurrent programming topics (a) - Concurrency and multithreading

Note: The computer must be single-core processor can not handle multiple tasks in parallel, multiple tasks can only be run concurrently on a single CPU. Similarly, the thread is the same, from a macro point of view to understand the thread is running in parallel, but the analysis from the microscopic point of view it is a serial operation, a thread that is a thread to run when the system is only one CPU, threads execute multiple threads in a certain order, we call this situation is called thread scheduling.

1.2 Sequence programming and concurrent programming

When we solve programming problems, usually solved using sequential programming, that is, all the things the program can only perform one step at any time. However, for certain issues, we hope to be able to execute multiple parallel part of the program, to achieve the effect we want. In a uniprocessor machine, we can program into a plurality of sections, and each section concurrently executed by the processor. In a multi-processor machines, we can program into a plurality of portions, each portion separately and then executed in parallel on multiple processors. Of course, in order to more fully utilize CPU resources, we can execute concurrently on multiple processors, so that we can relate to in another programming model of concurrent programming. Concurrent programming called multi-threaded programming. Concurrent programming so that our program can be divided into a number of separate, independent tasks running. By using multi-threading mechanism, each individual task will be to drive the thread. A thread is a single sequence in the process of flow control, a single process can have multiple "concurrent execution" task. This allows each task program, it seems to have its own CPU the same. But the underlying mechanism is still CPU time slicing, have a CPU clock frequency, expressed in number of instructions per second can be executed by the CPU. In each clock cycle, CPU really only possible to execute a plurality of instructions also. The operating system will manage the process, each process in turn assigned a short period of time but not necessarily sharing, and then within each process, the program code deal with their own internal allocation of time in the process thread mutually between multiple threads the switch to perform, this switching time is very short so usually we do not need to consider it.

Concurrency refers to the "fat", not treatment, the most common situation is that many people in a short period of time have clicked on your site, issued a processing request. Concurrent programming is to respond to concurrent conditions, can deal with them on a single processor and multi-processor machines can handle this program and about the architecture and algorithms. CPU is generally time-sharing, will be kept switched in a very short period of time to different threads, no matter how many concurrent process will go on, just a matter of time, how to improve the processing efficiency depends on the technology used.

1.3 concurrent programming advantages

If used properly, the thread can effectively reduce the cost of development and maintenance program, while improving the performance of complex applications. In particular, the advantages of thread are:

  • Play a powerful multi-processor capabilities
    now, multi-processor systems are becoming increasingly popular, and prices continue to decrease, in real-time and low-end servers interrupt desktop system, also typically employ multiple processors, this trend accelerated further, because to improve performance by increasing the clock frequency has become increasingly difficult, processor manufacturers are beginning to turn to place multiple processor cores on a single chip. Just think, if only a single thread on the system, the program can only use half of the CPU resources on a dual-core processor system with 100 processors are unable to use 99% of the resources. Multithreaded programs can be executed simultaneously on multiple processors, if properly designed, multi-threaded program can improve system throughput by increasing processor utilization of resources.

    • Higher throughput on a single processor system
      if the program is single-threaded, then the program waits for a synchronization when an I / O operation is complete, the processor will be idle. In a multithreaded program, if a thread is waiting for I / O operation is completed, another thread can continue to run, so that the program can continue to run during I / O blocked.

    • Modeling simplicity
      by the use of threads, it can be complex and asynchronously workflow further decomposed into a set of simple and synchronized workflow, each workflow runs a separate thread, and interact in a specific synchronization position. We can achieve these goals through a number of existing frameworks, such as Servlet and RMI, the framework is responsible for resolving some of the details, such as request management, thread creation, load balancing, and at the right time a request to the correct application components. Servlet developers to write not need to know how many requests to be processed at the same time, do not need to know the socket input stream or output stream is blocked, when you call the Servlet service method in response to a Web request, can be synchronized way to handle the request as if it is a single-threaded program.

    • Simplified process asynchronous events
      server application when a plurality of socket accepts a connection request from a remote client, if each thread is assigned to each connection and synchronous I / O, it will reduce the development of such procedures difficulty. If an application is performing a read operation on a socket at a time when no data arrives, then the read operation will block until data arrives. In a single-threaded application, this means not only in the course of processing a request stop, but also means that during this thread is blocked, the processing of all requests will pause. To avoid this problem, single-threaded server applications must use the non-blocking I / O, but the complexity of this I / O is much higher than synchronous I / O, and error prone. However, if each request has its own processing thread, so blocking occurs when processing a request will not affect the processing of other requests.

      In practical applications, multithreading is very useful, a browser must be able to download more than one picture; a Web server must be able to respond to multiple simultaneous user requests; Java virtual machine itself provides a super thread behind the scenes to garbage recycling; graphical user interface (GUI) applications also need to start a separate thread from the host environment to collect user interface events ...... in short, multi-threaded applications in the actual programming is very broad.

2. Processes and Threads

  • Process : refers to an application running in memory, each process has a separate memory space, an application can run multiple processes simultaneously; a process's execution is a program, running the program is the basic unit of the system; the system run a program that is created from a process, run to the demise of the process. The process can be understood as the basic operating unit managed by the operating system. 360 Browser is a process, WPS is a process, the operating system is running in ".exe" can be understood as a process.

Concurrent programming topics (a) - Concurrency and multithreading

  • 线程:线程是进程中的一个执行单元,负责当前进程中程序的执行,一个进程中至少有一个线程。一个进程中是可以有多个线程的,这个应用程序也可以称之为多线程程序。
    进程中独立运行的子任务就是一个线程。像QQ.exe运行的时候就有很多子任务在运行,比如聊天线程、好友视频线程、下载文件线程等等。
    Concurrent programming topics (a) - Concurrency and multithreading
    简而言之:一个程序运行后至少有一个进程,一个进程中可以包含多个线程。

    3.多任务、多进程、多线程

    几乎所有的操作系统都支持同时运行多个任务,一个任务通常就是一个程序,每个运行中的程序就是一个进程。当一个程序运行时,内部可能包含了多个顺序执行流,每个顺序执行流就是一个线程

    3.1 多进程

    实现并发最直接的方式是在操作系统级别使用进程,进程是运行在它自己的地址空间内的自包容的程序。多任务操作系统可以通过周期性地将CPU从一个进程切换到另一个进程,来实现同时运行多个进程。 尽管对于一个CPU而言,它在某个时间点只能运行一个进程,但CPU可以在多个进程之间进行轮换执行,并且CPU的切换速度极高,使我们无法感知其切换的过程,就好像有多个进程在同时执行。
    几乎所有的操作系统都支持进程的概念,所有运行中的任务通常对应一个进程(Process)。当一个程序进入内存运行时,即变成一个进程。进程是处于运行过程中的程序,并且具有一定的独立功能,进程是系统进行资源分配和调度的一个独立单位。一般而言,进程包含如下3个特征。

独立性:进程是系统中独立存在的实体,它可以拥有自己独立的资源,每一个进程都拥有自己私有的地址空间。在没有经过进程本身允许的情况下,一个用户进程不可以直接访问其他进程的地址空间。
动态性:进程与程序的区别在于,程序只是一个静态的指令集合,而进程是一个正在系统中活动的指令集合。在进程中加入了时间的概念,进程具有自己的生命周期和各种不同的状态,这些概念在程序中部是不具备的。
并发性:多个进程可以在单个处理器上并发执行,多个进程之间不会互相影响。

3.2 多线程
3.2.1 多线程概述

多线程则扩展了多进程的概念。使得同一个进程中也可以同时并发处理多个任务。线程(Thread)也被称作轻量级进程(Lightweight Process)。线程是进程的执行单元,就像进程在操作系统中的地位一样,线程在程序中是独立的、并发的执行流。当进程被初始化后,主线程就被创建了。对于绝大多数的应用程序来说,通常仅要求有一个主线程,但也可以在该进程内创建多条顺序执行流,这些顺序执行流就是线程,每个线程也是互相独立的。

线程是进程的组成部分,一个进程可以拥有多个线程,一个线程必须有一个父进程。线程可以拥有自己的堆栈、自己的程序计数器和自己的局部变量,但不拥有系统资源,它与父进程的其他线程共享该进程所拥有的全部资源。因为多个线程共享父进程里的全部资源,因此编程更加方便;但必须更加小心,我们必须确保线程不会妨碍同一进程里的其他线程。

3.2.2 多线程机制

Threading programming model brings convenience, it simplifies the handling of multiple operations in a single program simultaneously intertwined. When using threads, CPU will in turn assign it takes time to each task. Each task has been felt in the occupied CPU, but in fact the CPU time is divided into segments assigned to all tasks. One big advantage is that thread can make you withdraw from this level, that is, the code does not have to know that it is running on a machine with one or more than one CPU. Therefore, the use of threads is a mechanism to establish transparent, scalable method for the program, if the program line too slowly, adding a CPU as the machine can easily run faster program. Multi-tasking and multi-threaded multiprocessor systems often use the most reasonable way.

3.2.3 Multi-thread scheduling

Thread can complete a certain task, you can share the shared variable part of the parent process and environment and other threads, synergy with each other to complete the process to complete the task. A thread is run independently, it does not know whether there are other threads in the process, execution threads is preemptive, that is, the currently running thread may be suspended at any time, so that another thread can run .

A thread can be created and destroyed another thread can execute concurrently across multiple threads in the same process. From a logical point of view, it exists in a multi-threaded application, so that an application can have multiple simultaneous implementation of the operative part, but the operating system will need multiple threads regarded as multiple independent applications for multithreading scheduling and management and resource allocation. Thread scheduling and management responsible for the completion of the process itself.

Carolina from mining can say this: the operating system can perform multiple tasks simultaneously, each task is the process; the process can perform multiple tasks simultaneously, each task is to thread. In short, after a program has run at least one process, in a process can contain multiple threads, but must contain at least one thread.

Guess you like

Origin blog.51cto.com/13819911/2404176