Concurrent Programming (1)-concurrency Overview

Source: Internet
Author: User
Concurrent Programming is an advanced Required Course for every programmer. It is easier to write a secure and stable concurrent program with strong performance. In the future, I will share with you what you think about the growth of concurrency. The idea of concurrent programming is fluent, but the example must be used as an example. In this series, Java is used for demonstration. This article is the first article in the series of concurrent programming. Because I like to talk about concurrent programming first, rather than first, I start with a description of concurrent texts.
Concurrent ProgrammingIn the early years, there was no operating system on the computer. in a certain period of time, only one program can be run, and the program can access all the resources of the computer. After this program is fully executed, execute the next program.
At this time, introduce the advantages of concurrent programming:
  • Efficiency: do not have to worry about the various components of the computer. For example, when a program uses Io, the CPU can be used by another program. This increases the device usage.
  • Fairness: the program running on the computer should be treated equally, rather than executing another program after the first execution.
  • Convenience: When computing multiple tasks, you should write multiple programs and make them communicate with each other if necessary, which is easier to implement than one program to implement multiple tasks.
The earliest manifestation of concurrent programming is multi-process concurrent programming, which was introduced by the emergence of the operating system. Concurrent multi-process programming solves the problem that the process occupies all the devices of the computer and increases the throughput of the computer. We can imagine that in this case, the correspondence between computer devices and running processes at any time is one-to-one ,:. If the number of processes currently running is small, many devices in the system will remain idle. In this case, the use of threads can further improve the processing efficiency of the computer. A thread allows multiple program control flows in a process, then multiple threads of a process can run on multiple devices at the same time. Therefore, using threads can further improve the throughput and processing efficiency of the system. Now, with the realization of Moore's Law, the increasing performance of a single CPU is gradually transferred to the increasing number of cpus. It is very important to improve the resource utilization of multiple CPUs, these reasons make concurrent programming of threads more and more important. (From the above analysis, to improve the performance of multi-threaded programs from the underlying layer, we should try to make multiple threads run on multiple devices at the same time .)
Simplicity of ModelingIn addition to improving performance, threads can further break down complex and asynchronous workflows into a set of simple and synchronous workflows. Each workflow runs in a separate thread, and interact with each other at a specific synchronization location. This actually affects the use of multithreading technology, that is, it usually uses multiple threads to implement logically or physically independent tasks (such as computing tasks and IO tasks ), not in the program.

Risks brought by threads
  • Security: in many cases, threads are not completely independent, but collaborate to complete a task. Therefore, you may need to access one or more resources together, when there is no proper collaboration between threads, it may lead to confusion of a single resource or data consistency between multiple resources.
  • Active problem: sometimes it is often necessary to control the execution sequence of multiple threads. For example, one thread reads Io and the other thread obtains from the previous thread. Therefore, we need to control the synchronization of the thread. The collaboration between threads can easily lead to problems such as deadlocks and hunger. The thread becomes inactive ".
  • Performance problems: frequent thread switching and synchronization mechanisms (often suppressing Compiler Optimization and invalidates data in the memory cache ). A question is raised here: the thread can execute some composite operations, and the usage is not much different from the process. Why do we further segment the thread, for example, if a scheduling unit "small thread" is created, will the execution efficiency of the program be lower than that of the thread? A: This will further increase the number of program control flows and lead to more frequent thread switching. In fact, dividing the thread layer may be the best compromise.

Ubiquitous threads Multi-thread scenariosThe most common scenarios requiring multi-threaded programming are: 1. there are multiple logical or physical tasks that need to be executed. Separate them with multiple threads instead of completing all the work in one thread, the main benefit of using multithreading here is to make the program structure clearer and simplify programming. 2. split a task into several smaller tasks, or divide a task into several parts for execution by multiple threads, such as computing tasks, the main purpose of Multithreading is to improve the performance.
Scenarios where thread-safe code needs to be compiledIn addition to actively writing multi-threaded programs to "attack", we often need to "Prevent" multithreading to prevent some derivative dangers caused by multithreading.
  • Timer: we write our own scheduled tasks, and the threads managed by timer will call them. Other threads are also very likely, so pay attention to thread security here.
  • Servlet and JSP: Usually our Servlet and JSP will be called by multiple threads at the same time, and the web server will allocate a thread to each incoming HTTP request from the thread pool. Therefore, in Servlet and JSP, we need to ensure thread security. Similarly, the servlet filters and objects stored in containers such as servletcontext and httpsession must be thread-safe.
  • Remote method call (RMI): in RMI, there is a possibility of being re-imported by multiple threads, so they must ensure their own thread security.


Concurrent Programming (1)-concurrency Overview

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.