Java concurrency - first knowledge + challenges caused by concurrent programming

In order to make the program run faster, multi-threading is introduced. It does not mean that enabling more threads will increase the running speed of the program. Concurrent programming presents many challenges. Such as context switching, deadlock problems.

context switch

The CPU allocates threads to complete tasks through time slice transfer. When a time slice is completed, it will switch to the task of another thread. At this time, it is necessary to save the environment of the unfinished task, etc. The knowledge of the operating system is stored in the pcb so that it can continue when switching back to this thread. , this process is called a context switch.

How to switch context is the key for multi-threads to complete tasks efficiently.

Multi-threading is not necessarily faster than single-threading. When the cumulative number of concurrent operations does not exceed one million, it is slower than serial. The reason is the overhead of context switching and thread creation.

There are several tools for testing in context.

Lmbench3 can test the duration of context switching

vmstat tests the number of context switches

Reduce context switching

Lock-free concurrent programming, CAS algorithm, using minimal threads, using coroutines

Lock-free concurrent programming

Use Hash to segment the data ID, and different threads process different data

CAS algorithm

Atomic's CAS algorithm does not require locking. A method of optimistic lock implementation, but it may spin for a long time, increasing the CPU overhead. The principle is to check whether it is the same as the previous value before updating the data, and if so, update the original value. Theoretically, three values ​​are needed, a memory value, the expected original value, and the new value. If the expected original value is the same as the memory value, update the new value to the memory.

Problems with cas:

  1. ABA: That is to say, because the CAS algorithm will compare values ​​before updating, the two values ​​​​look the same when compared, for example, they are both a, but it is possible that they have been changed to b and then back to a during the process. Then cas It will be considered that it has not been changed, and thus problems arise. The solution to this problem is to use the version number mechanism, which means that each change leaves a version number.
  2. Long-term cyclic spin: This means that CAS may not be successful for a long time, and it will spin to increase the CPU overhead.
  3. He can only guarantee the atomic operation of a shared variable at the same time. However, the emergence of the AomicReference class after 1.5 ensures the atomicity of the referenced object. Multiple shared variables can be placed in the same object for CAS operations. 

Use minimal threads

Avoid creating unnecessary threads.

coroutine

Implement multi-task scheduling in a single thread and maintain the switching of multiple tasks.

deadlock

According to the definition of the operating system: when a thread waits indefinitely for a resource occupied by another thread that will not be released, it will cause a deadlock problem. When a deadlock occurs, you can use the dump thread to see where the deadlock occurred.

Several ways to avoid deadlock:

  1. Avoid threads acquiring multiple locks at the same time
  2. Prevent one thread from occupying multiple resources at the same time in the lock
  3. use time lock
  4. For database locks, locking and unlocking must be done in a database connection.

resource constraints

For example, some hardware problems.

Guess you like

Origin blog.csdn.net/Yoke______/article/details/123228507