Java multithreading learning road (four) --- deadlock (DeadLock)

Java multithreaded learning road (four)-deadlock (DeadLock)

1. Definition

Deadlock is a state in which multiple threads block each other and cannot get out (personal understanding) when multiple threads compete for shared resources. In fact, deadlock can be regarded as an infinite loop to a certain extent.

As an example in real life, on a narrow lane in one direction, two batches of traffic came from both directions at the same time, and they met in the middle of the road. Both parties must wait for the other to pass first. The last two batches of traffic are only Can be consumed in the middle of the road.

In multi-threaded programming, if Thread1 owns the lock of Resource1 and applies for the lock of Resource2 at the same time, at the same time, Thread2 owns the lock of Resource2 and applies for the lock of Resource1 at the same time. Both parties can only wait for the other to release the lock after the execution of the other party to continue execution ... eventually lead to deadlock.

Insert picture description here

2 Necessary conditions for deadlock (note that it is not a necessary and sufficient condition)

  1. Mutex ( Mutual Exclusion ) : at least one resource in a non-shared mode, that is, a resource can only use one thread
  2. Possession and wait for ( the HOLD and the wait ) : a thread when applying for a shared resource, it should at least occupy a shared resource, and will not release
  3. Non-preemption ( no preemption ) : Shared resources cannot be preempted unless the current thread releases its object lock after completing the task
  4. Circular wait ( circular wait ) : a cycle of possession and application

Emphasize: meeting the above four conditions does not necessarily cause deadlock, they are only necessary conditions

3. How to solve the deadlock

  1. Prevent deadlock : make deadlock never happen.
  2. Avoid deadlock : Prevent operations that will cause deadlock.
  3. Allow deadlock to occur, destroy and restore deadlock .
  4. Ignore the deadlock (also known as the ostrich algorithm) and simply ignore it.

In fact, most OSs (Windows, Linux, etc.) choose the fourth option, which is to ignore deadlocks . Throw the problem to the application developer. Therefore, if there is a problem with Windows, the first thing is to restart (dog head).

4. Prevent deadlock

Preventing deadlock is actually very simple, that is, destroy any one of the four necessary conditions.

  • Destruction of mutual exclusion: Set resources to be non-mutually exclusive, that is, multiple threads can access at the same time.
  • Destroy possession and waiting: There are two ways to destroy 1. Each thread directly applies for all required resources before execution. This way, it will not be executed halfway and then apply for other resources, but this requires it to be at the beginning Know what resources you need before execution. 2. When each thread applies for a resource, it needs to ensure that it does not occupy other resources. If it does, it must first release the occupied resources. But this may also be problematic , Then maybe it needs two resources to execute at the same time.
  • Destruction of non-preemption: There are two ways: 1. If a thread that has already occupied a certain resource fails to apply for other resources, the resources originally occupied by the thread can be preempted (equivalent to being released invisible), only when The thread can only re-run when it obtains the original resources and the new resources requested. 2. If a thread Thread1 is applying for a resource that is being occupied by another thread Thread2, the OS can force Thread2 to release the resources required by Thread1. (The premise is that the thread priority of Thread1 is higher)
  • Destruction cycle waiting: All resource types are completely sorted, and all threads are required to apply in the order of resources. For example, you can see the deadlock example above. If you sort the resources and apply for resources in order, then Thread1 and Thread2 are required. Resource1 and Resource2, but only in the order of applying for Resource1 and then applying for Resource2, so that such problems will not occur

5. Recover deadlock

6. Ignore deadlocks

Guess you like

Origin blog.csdn.net/qq_44823898/article/details/109549757