Locks are a very useful tool that can be used in many different ways because they are very simple to use and easy to understand. However, it can also cause some problems, that is, it can cause deadlocks, once deadlocks occur, the system function is not available.

The necessary conditions for a deadlock to occur

  1. Mutually exclusive condition: a resource can only be used by one process at a time.
  2. Request and hold conditions: when a process is blocked by requesting resources, it holds on to acquired resources.
  3. Non-dispossession condition: a process cannot forcibly take away a resource it has acquired until it is used up.
  4. Circular waiting condition: a circular waiting resource relationship is formed between several processes.

Popular awards are:

  1. Mutually exclusive condition: a resource can only be used by one process at a time. The log can only pass one person at a time.
  2. Request and hold conditions: when a process is blocked by requesting resources, it holds on to acquired resources. B does not exit the deck, and A does not exit the deck.
  3. Non-dispossession condition: a process cannot forcibly take away a resource it has acquired until it is used up. A cannot force B to exit the bridge deck, nor can B force A to exit the bridge deck.
  4. Circular waiting condition: a circular waiting resource relationship is formed between several processes. If b does not exit the deck, A cannot pass, and if A does not exit the deck, B cannot pass.

The main cause of deadlocks

1. Competition for system resources The amount of inalienable resources in the system is insufficient to meet the requirements of multiple processes. As a result, processes are in deadlock due to competition for resources, such as tape drives and printers. Only competition for inalienable resources can cause deadlocks, but competition for disposable resources cannot cause deadlocks. When an illegal process is running, the sequence of requesting and releasing resources is not correct, which also leads to a deadlock. For example, concurrent processes P1 and P2 hold resources R1 and R2, respectively. When process P1 applies for resource R2, and process P2 applies for resource R1, both processes are blocked because required resources are occupied. In the simplest case of deadlock in Java, one thread T1 holds lock L1 and applies for lock L2, while another thread T2 holds lock L2 and applies for lock L1. Since lock applications are blocked by default, threads T1 and T2 are permanently blocked. The result is a deadlock. This is the easiest and simplest form of deadlock to understand. But deadlocks in real environments are often more complex than this. For example, thread T1 holds lock L1 and is applying for lock L2, thread T2 holds lock L2 and is applying for lock L3, and thread T3 holds lock L3 and is applying for lock L1, resulting in a lock-dependent loop: T1 depends on lock L2 of T2, T2 on lock L3 of T3, and T3 on lock L1 of T1. This leads to deadlocks.

Common ways to avoid deadlocks

  • Avoid one thread acquiring multiple locks at the same time
  • Avoid a thread occupying multiple resources in a lock at the same time. Try to ensure that each lock occupies only one resource
  • Try using the timing locklock.tryLock(timeout)Instead of using an internal locking mechanism
  • For database locks, lock and unlock must be in the same database connection, otherwise the unlock will fail