Java-17.4 synchronization (5)-deadlock
In this chapter, we will briefly discuss the deadlock. In fact, deadlock is a big topic, but here I will explain it based on the example of bank transfers I mentioned earlier, A detailed column will be provided later to illustrate all aspects of concurrent programming.
1. What is a deadlock?
It refers to a blocking phenomenon caused by competition of resources or communication between two or more processes during execution. If there is no external force, they will not be able to proceed.
2. How can it cause a deadlock?
(1) mutex condition: A resource can only be used by one process at a time;
(2) Conditions for request and Retention: the application is not always blocked because the existing resources are used and other resources are applied;
(3) Non-deprivation conditions: resources with the right to use resources cannot be deprived by priority;
(4) Cyclic waiting condition: a cyclic waiting resource relationship is formed between several processes that are connected at the beginning and end.
3. How did the previous example cause a deadlock?
(1) deadlock generation process:
Account 1 original 200 yuan
Account 2 original 300 yuan
Thread 1: switch from account 1 to account 2
Thread 2: switch from account 2 to 400 yuan to account 1
(2) code blocks that cause deadlocks:
Public void transfer (int fromAccount, int toAccount, double money) {if (accounts [fromAccount] <money) {System. out. println (Thread. currentThread (). getName () + "deadlock"); return;} accounts [fromAccount]-= money; System. out. printf ("transferred from" + fromAccount + "account % 10.2f RMB,", money); accounts [toAccount] + = money; System. out. printf ("transferred from" + toAccount + "account to % 10.2f RMB,", money); System. out. printf ("Total: % 10.2f RMB", getTotal (); System. out. println ();}
(3) cause:
When the money transferred exceeds the existing balance, a deadlock will occur.
(4) solution:
However, because the actual business does not allow this situation, we can directly return to solve the deadlock problem.
(5) If it is other multi-threaded competing resources, it is not the same as the above return, and wait, there will be a deadlock.
4. Complete example:
Package com. ray. ch17; public class Bank {private final double [] accounts; public double [] getAccounts () {return accounts;} public Bank (int n, double initBalance) {accounts = new double [n]; for (int I = 0; I <accounts. length; I ++) {accounts [I] = initBalance ;}} public double getTotal () {double total = 0; for (int I = 0; I <accounts. length; I ++) {total + = accounts [I];} return total;} public void transfer (int fromAccount, int toAccount, double money) {if (accounts [fromAccount] <money) {System. out. println (Thread. currentThread (). getName () + "deadlock"); // Thread. currentThread (). wait (); // if it is other multithreading, it may be wait here, resulting in a real deadlock return;} accounts [fromAccount]-= money; System. out. printf ("transferred from" + fromAccount + "account % 10.2f RMB,", money); accounts [toAccount] + = money; System. out. printf ("transferred from" + toAccount + "account to % 10.2f RMB,", money); System. out. printf ("Total: % 10.2f RMB", getTotal (); System. out. println ();} public int size () {return accounts. length ;}}
package com.ray.ch17;import java.util.Random;public class TransferThread implements Runnable {private Bank bank;private final double MAX;public TransferThread(Bank bank, double max) {this.bank = bank;this.MAX = max;}@Overridepublic void run() {while (true) {double amount = MAX * Math.random();int countOfAccount = bank.getAccounts().length;bank.transfer(new Random().nextInt(countOfAccount),new Random().nextInt(countOfAccount), amount);}}}
package com.ray.ch17;public class Test {public static void main(String[] args) {Bank bank = new Bank(100, 10000);for (int i = 0; i < 2; i++) {TransferThread transferThread = new TransferThread(bank, 10000);Thread thread = new Thread(transferThread);thread.start();}}}
Conclusion: This chapter briefly introduces deadlock.