Concurrent Programming (1) ---- Concurrent Programming basics, ---- programming Basics

Source: Internet
Author: User

Concurrent Programming (1) ---- Concurrent Programming basics, ---- programming Basics

Multithreading is a basic knowledge that programmers must master. However, due to the encapsulation of various frameworks, we seldom access multithreading. Once a problem occurs in the project, you cannot start. Multi-threading not only helps you better handle project problems, but also helps you understand some frameworks. This series is based on the "Java concurrent programming art", combined with some online materials and their own understanding. If any error occurs, please correct it.

1. Several Concepts

1) Process: A running activity of a program with certain independent functions regarding a dataset. A process is an independent unit for the system to allocate and schedule resources. An operating system can run multiple tasks (programs) at the same time. Each running task (Program) is called a process. For example, if you start a Java program, the operating system will create a Java Process.

2) thread: it is an entity of a process and the basic unit of CPU scheduling and dispatching. It is a basic unit that can run independently less than a process. 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 that belong to the same process.

3) online text switching:

Even a single-core processor supports multi-threaded code execution. The CPU allocates CPU time slices to each thread to implement this mechanism. The time slice is the time that the CPU allocates to each thread. Because the time slice is very short, the CPU continuously switches the thread for execution, making us feel that multiple threads are executed at the same time.

The CPU uses the time slice allocation algorithm to execute tasks cyclically. After the current task executes a time slice, it switches to the next task. However, the status of the previous task is saved before the switch, so that the status of the task can be loaded the next time you switch back to the task. Therefore, the process from saving to reloading a task is a context switch.

This is like reading two books at the same time. When reading an English Technical book, we find that a word is not recognized, so we open the dictionary to search for it, however, you must remember the number of pages that the book has read before searching. You can continue to read this book after checking the words. Such switching will affect the reading efficiency, and context switching will also affect the execution efficiency of multiple threads.

2. Why multithreading?

1) Make full use of system resources

Moore's law is getting longer and longer, and the way to improve the performance of the processor is also evolving from a higher frequency to a multi-core. A thread is the basic unit for most operating system scheduling, and a thread is an entity of a process. A thread can only run on one cpu at a time, thus making full use of system resources. If a computing task is assigned to multiple processor cores, the processing time of the program is significantly reduced.

2) faster response time

For example, in some complex businesses, the server needs to perform a series of processing procedures to return the final results to the client starting from clicking the button. With multithreading technology, the execution results can be returned to the client after data processing with high real-time requirements is completed. Some operations with poor data consistency are distributed to other threads for processing (such as Message Queue/Worker ). This can shorten the response time and improve the user experience.

3) Better programming model (bionic)

Java provides a well-developed and consistent programming model for multithreading, allowing developers to focus more on problem solving. For example, in android game development, the work of controlling a person is coordinated by multiple threads, which is hard to be achieved if multithreading is used.

3. multithreading Problems

  In concurrent programming, two problems need to be solved: how to communicate between threads and how to synchronize between threads. Communication refers to the mechanism by which threads exchange information, and synchronization refers to the mechanism used in the program to control the relative execution sequence between different threads.

1) There are two communication mechanisms between threads: Shared Memory and message transmission.

In the concurrency model of shared memory, threads share the public state of the program and perform implicit communication through the public state in the write-read memory. In the concurrency model of message passing, there is no public state between threads. Communication between threads must be displayed by sending messages (for example, the wait () of the Object in Java () with notify ()).

2) Synchronization Mechanism

In concurrent programming, especially when processing shared resources, some operations (such as write operations) need to be converted to synchronization to avoid unimaginable consequences. There are two common synchronization mechanisms: the JVM layer, which is implemented by using the Object monitor (synchronized) and the bus lock or locking part of the memory, such as volatile.

4. concurrent programming challenges

1) context switch.

When there are a large number of threads, frequent online text switching will lead to lower execution efficiency, and reasonable adjustment of the number of threads can maximize the efficiency of concurrent execution.

2) deadlock.

Locks are very useful tools and are used in many scenarios. However, improper use may lead to deadlocks, which may paralyze the system in severe cases. A deadlock creates two threads, A and B. a has lock a. At the same time, it needs to obtain the lock B. B has the lock B. At the same time, it also needs to obtain the lock. Both hold the lock and cannot obtain the required lock. In actual development, the deadlock should be avoided.

Common methods to avoid deadlocks

A. Avoid a thread from simultaneously obtaining multiple locks.

B. One thread occupies multiple resources in the lock at the same time, and each lock occupies only one resource as much as possible.

C. Try to use the timed lock and use lock. tryLock (timeout)

D. handle exceptions in the program to ensure that the lock can be released normally when exceptions occur in the program.

D. For database locks, the locking and unlocking must be in the same database connection; otherwise, the unlocking may fail.

3) resource restrictions.

Resource Restriction refers to the limitation of computer hardware resources or software resources in concurrent programming. For example, a server has a loan of only 2 M/s. When multithreading is used, the download speed is not increased. Due to resource limitations, the thread is still executed in serial mode. The program slows down because of context switching and resource scheduling time.

For hardware resources, you can increase the bandwidth or cluster. For software resources, such as database connections, you can maximize resource utilization by controlling the number of concurrent threads.

 

 

  

  

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.