Starting from today, Java concurrent Programming Art learning, each chapter after learning to record the focus of the content here, make a note, deepen the impression.
The purpose of concurrent programming is to make the program run faster, but not to start more threads that will allow the program to execute as much as possible concurrently. Concurrency is that if you want your program to run faster through multi-site execution, there are a number of challenges, such as context switching issues, deadlock issues, and resource constraints constrained by hardware and software, which describe several concurrent programming challenges and Solutions
1, the upper and lower ask to switch
Even a single-core processor supports multithreaded code execution, which is implemented by the CPU by allocating CPU time slices to each thread. The time slice is the time the CPU allocates to each thread, because the time slice is very short, so the CPU keeps switching threads to execute, let us feel that multiple threads are executed at the same time, the time slice is generally dozens of milliseconds.
The CPU loops through the time slice allocation algorithm to perform the task, and the current task executes a time slice and switches to the next task. However, the status of the previous task is saved before switching, so that the task status can be loaded again the next time you switch back to the task. So the process of a task from saving to reloading is a context switch.
It was like we were two books at the same time, when we read a technical book in English, we found that a word did not know, so we opened the English-Chinese dictionary, but before we put down the English technical book, the brain must first remember how many pages the book read, and after checking the dictionary, can continue to read the book. Such a switch can affect the efficiency of reading, and the same context switch will affect the speed of multi-threaded execution.
How do I reduce context switching?
The methods for reducing context switching are lock-free concurrent programming, CAS algorithms, using minimal threads, and using the coprocessor.
No lock concurrent programming. Multi-threaded competition lock, will cause context switching, so multithreading data, you can use some methods to avoid the use of locks, such as the ID of the data in accordance with the hash algorithm segmentation, different threads processing different data.
CAS algorithm. The Java atomic package uses the CAS algorithm to update data without locking.
Use minimal threads. Avoid creating unwanted threads, such as fewer tasks, but creating a lot of threads to handle, which can cause a lot of threads to wait.
Co-process. Multitask scheduling in a single thread and multiple tasks in a single thread
2. Deadlock
Here are just a few common ways to avoid deadlocks.
Prevent a thread from fetching multiple locks at the same time.
Avoid a thread that consumes more than one resource at a time, and try to ensure that each lock occupies only one resource.
Try using a timed lock, using Lock.trylock (timeout) instead of using the internal lock mechanism.
For a database lock, the lock and unlock must be in a database connection, or there will be a failure to unlock the situation.
3. Challenges of resource constraints
what are resource limits ?
Resource throttling refers to the speed at which programs execute when concurrent programming is restricted to computer hardware resources or software resources. Hardware resources are limited to broadband upload/download speed, hard disk read and write speed, and CPU processing speed. The software restricts the number of connections to the database and sockets.
Issues raised by resource constraints
In concurrent programming, the principle of speeding up code execution is to make the part of the code serially executed concurrently, but if a piece of serial code is executed concurrently, because the resource is still in serial execution, the program not only does not speed up execution, but is slower, because it increases the time for context switching and resource scheduling.
How to solve the resource limitation problem
For hardware resource limitations, consider using a cluster parallel execution program. Since the single-machine resources are limited, so long let the program run on multiple machines, different machines processing different data. A machine number can be calculated by "Data id%", and the data is processed by the corresponding numbered machine.
For software limitations, consider using a resource pool to reuse resources. For example, using a connection pool to reuse a database and socket connection, or to call the other WebService interface to obtain data, only one connection is established.
Concurrent programming in resource-constrained situations
Adjust concurrency based on different resource constraints, such as downloading a file program that relies on two resource bandwidth and hard disk read and write speeds. When there is a database operation involving the number of data connections, if the SQL statement executes very quickly and the number of threads is much larger than the number of database connections, some threads are blocked, waiting for the database to connect
Java Concurrent Programming Art one (the challenge of concurrent programming)