This is the 8th day of my participation in the August More Text Challenge

A deadlock

  • A deadlock is when two or more threads are blocked waiting to acquire a lock held by some other thread lock in a deadlock state. Deadlocks can occur when multiple threads need to acquire the same lock but in a different order.

A deadlock, for example,

  • When thread A locks object lock1, it tries to acquire object lock2; At the same time, thread B locks the object lock2 and tries to acquire the object lock1. In this case, thread A will lock lock1 forever and cannot acquire lock2, thread B will acquire lock2 forever and cannot acquire lock1, and thread A and thread B will enter a deadlock state.
Object lock1;
Object lock2;
new Thread(() -> {
    synchronized(lock1){
        synchronized(lock2){
            
        }
    }
},"thread a").start();
new Thread(() -> {
    synchronized(lock2){
        synchronized(lock1){
            
        }
    }
},"thread b").start();
Copy the code
  • The prerequisite for a deadlock is that both lock1 and LOCK2 are locked at the same time. In this way, both objects enter the wait state and the deadlock occurs. If the lock time of LOCK1 and lock2 is different, the deadlock may not occur due to execution sequence.

Deadlocks in complex states

  • When a deadlock contains more than two threads, a more complex deadlock is formed.
Thread 1 locks A, waits for thread 2 to lock B, waits for thread 3 to lock C, waits for thread 4 to lock D, waits for thread ACopy the code
  • Thread 1 waits for thread 2, thread 2 waits for thread 3, thread 3 waits for thread 4, and thread 4 waits for thread 1.

Database deadlock

  • Because of the special nature of database transactions, more complex deadlocks can occur.
  • When a database record is updated during a transaction, the record is locked by the database until the transaction completes. Therefore, each update request in the same transaction may lock several rows of the database.
  • Complex database transaction deadlocks can occur if multiple transactions are running at the same time and all need to update the same record in the database.
Transaction 1, request 1, locks record 1 for update transaction 2, request 1, locks record 2 for update transaction 1, request 2, tries to lock record 2 for update. Transaction 2, request 2, attempts to lock record 1 for updates.Copy the code
  • Locks are acquired on separate requests, so database locks are not known in advance and are difficult to prevent.

Deadlock prevention – Lock sort

  • A deadlock occurs when different threads acquire the same lock in a different order; if the same order is acquired, no deadlock occurs.
Thread 1:
  lock A 
  lock B

Thread 2:
   wait for A
   lock C (when A locked)

Thread 3:
   wait for A
   wait for B
   wait for C
Copy the code
  • When a thread holds multiple locks, they must be acquired in a fixed order. If the 3-wire program obtains lock C first, it will cause a deadlock. Sorting locks is a simple and effective way to avoid deadlocks, but it can only be used if you have all the locks you need to know about any lock.

Deadlock prevention – Lock timeout

  • Another way to prevent deadlocks is through the lock timeout mechanism. When a thread repeatedly tries and fails to acquire a lock, it backs up and releases all locks, waiting for a random time to retry.
  • Lock timeout cannot be usedsynchronizedKeyword that requires a custom lock or java.util.concurrencyConstructs in packages.

Deadlock detection

  • Deadlock detection is a more complex mechanism for deadlock prevention. This is noted in the data structure each time a thread acquires a lock. When a lock is requested but denied, you can traverse the lock graph to check for deadlocks.
  • What the thread does if a deadlock is detected:
    • Back up, release all locks, and wait for a random time
    • Determine the priority of the thread

Afterword.

  • How many events through the ages? Leisurely. The Yangtze River is still rolling.