Ramble on concurrent Programming (i)-Concurrency Brief introduction

Source: Internet
Author: User

      Concurrent programming is a required lesson for every program ape advanced, and to write a secure and stable, powerful concurrency program is not so easy. I will share with you in the coming days aConcurrent Small whiteThe thought of growing up on the road.     The idea of concurrent programming is a pass, but if the sample is present, the Java language will be used in the series for demonstration. This article, as the first of the series of concurrent programming, probes into the origin of this article, and begins with a descriptive narrative of concurrency.
The origin of concurrent programmingThere was no operating system in the early days of the computer, only one program was supported for a certain period of time, and the program was able to access all the resources of the computer. After the program has completely run, run the next program.
At this point, the advantages of introducing concurrent programming are:
    • Efficiency: The various parts of the computer are not busy, such as a program when using IO, the CPU can be used for another program. This increases the usage of the device.
    • Fairness: Programs running on the computer should be treated equally, not one after the first run, and then another program to start running.
    • Convenience: When calculating multiple tasks, it is easier to write multiple programs and, if necessary, communicate with each other, rather than having a single program implement multiple tasks.
The earliest manifestation of concurrent programming is multi-process concurrent programming, which is introduced by the advent of the operating system. Multi-process concurrent programming overcomes the problem of all the devices in the process, increasing the throughput of the computer. The ability to imagine a random moment in such a situation where the computer device is related to the process being executed is one-to:. Assuming that the current execution process is small, the system's very many devices will still be in the spare state, at this time, the use of threads can further improve the processing efficiency of the computer, the thread agrees that a process has multiple program control flow, then a process of multiple threads can be executed on multiple devices at the same time, see: So using threads can further improve the throughput and processing efficiency of the system. And now, with the implementation of Moore's law gradually shifted from single CPU performance to the number of CPUs to add, how to improve the resource utilization of multi-CPU becomes very important, these reasons make the thread's concurrent programming more important. (from the above analysis, it is possible to improve the performance of multithreaded applications from the underlying to enable multiple threads to execute on multiple devices at the same time.) )
The simplicity of modelingin addition to improving performance, threads can further decompose complex and asynchronous workflows into a simple and synchronized set of workflows, where each workflow executes in a separate thread and interacts at a specific synchronization location. This article actually affects the use of multithreaded technology, which usually logically or physically separate tasks with multithreading techniques (such as computational tasks and IO tasks) rather than interleaving in the program.

the risks that the thread poses
    • Security issues: In many cases, threads are not completely independent, but work together to complete a task, so it may be necessary to have a common access to one or more resources, which can result in a single resource confusion or a problem of data consistency across multiple resources when there is no proper collaboration between threads.
    • Active issues: Sometimes it is often necessary to control the order in which multiple threads are run, such as a thread reading IO, and one thread taking from the previous thread, so we need to control the thread synchronously. Collaboration between threads, easy leads to deadlocks, starvation and other issues. The thread will become inactive.
    • Performance issues: Frequent thread switching, synchronization mechanisms (often suppressing compiler optimizations to invalidate data in memory buffers). Here is a question: the thread is able to run a number of composite operations, in the use of the way and the process is not very different, why we are not on the basis of a further subdivision of the thread, for example, to make a more granular scheduling unit "Small thread", such a program will not run more efficient? A: This will further add the number of program control flows, which will lead to more frequent thread switching, and the fact that the layer is divided into threads is perhaps the best compromise.

Ubiquitous Threads using multi-threaded scenariosThe most common scenarios where you need to write a multithreaded program are: 1. There are multiple logical or physical tasks that need to be run, differentiate them with multiple threads instead of all in one thread, and the main advantage of using multithreading here is to make the program structure clearer and simplify programming. 2. Splitting a task into smaller tasks, or dividing a task into several parts, and handing it over to multiple threads, such as computational tasks, the main purpose of using multithreading at this time is to improve performance.
scenarios where you need to write thread-safe codeIn addition to actively writing multi-threaded programs to "attack", we often need to "anti-threading", to prevent some of the derivative harm of multithreading.
    • Timer: We write our own timed tasks, and the timer manages the threads that are called. Other threads are also very likely, so be aware of thread safety in this.
    • Servlets and JSPs: Usually our servlets and JSPs are called by multiple threads at the same time, and webserver assigns a thread to each incoming HTTP request from the thread pool. So we need to ensure thread safety in servlets and JSPs, and in the same vein, the servlet filters and objects stored in containers such as ServletContext and httpsession must be thread-safe.
    • Remote Method Invocation (RMI): In RMI, there is the possibility of being re-entered by multiple threads, so you must ensure that they are thread-safe.


Ramble on concurrent Programming (i)-Concurrency Brief introduction

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.