Java multi-threaded multi-person transfer deadlock

Deadlock occurs in multiple transfers

When the number of people is large, deadlock will still occur, follow Murphy's law.
Although the probability of deadlock when there are many people is not high, it is harmful. In
the code in this section, you need to use the code in the previous section, and the link is as follows .
https://javaweixin6.blog.csdn.net/article/details/108475207

The code in this section is as follows:
define an array of accounts, which are used to transfer funds to different accounts.
And the initial value of money for each account is 1000.
In the TransferThread internal class, the Thread class is inherited, the run method is rewritten, and the number of times to execute NUM_ITERATIONS The transfer operation of each transfer. And each transfer of the recipient, the transfer party, the transfer amount is random. Call the TransferMoney.transferMoneytransfer method in the run method

Then open NUM_THREADS (20) threads to execute the tasks in the run method

package com.thread.deadlock;

import java.util.Random;

/**
 * 类名称:MultiTransferMoney
 * 类描述: 多人转账发生死锁demo
 *
 * @author: https://javaweixin6.blog.csdn.net/
 * 创建时间:2020/9/9 7:13
 * Version 1.0
 */
public class MultiTransferMoney {
    
    

    //账户的总数量
    private static final int NUM_ACCOUNTS = 500;

    //每个账户初始的余额
    private static final int NUM_MONEY = 1000;
    private static final int NUM_ITERATIONS = 1000000;
    private static final int NUM_THREADS = 20;

    public static void main(String[] args) {
    
    
        Random random = new Random();

        //定义转账的账户数组
        TransferMoney.Account[] accounts = new TransferMoney.Account[NUM_ACCOUNTS];

        for (int i = 0; i < accounts.length; i++) {
    
    
            //给每个账户数组中的元素定初始值
            accounts[i] = new TransferMoney.Account(NUM_MONEY);
        }

        class TransferThread extends Thread {
    
    

            @Override
            public void run() {
    
    
                //每一个线程都进行随机的转账

                for (int i = 0; i < NUM_ITERATIONS; i++) {
    
    
                    //随机获取转账方索引
                    int fromAccount = random.nextInt(NUM_ACCOUNTS);
                    //随机的获取收款方
                    int toAccount = random.nextInt(NUM_ACCOUNTS);
                    //随机获取转账金额
                    int amount = random.nextInt(NUM_MONEY);

                    //执行转账的方法
                    TransferMoney.transferMoney(accounts[fromAccount],accounts[toAccount],amount);

                }

            }
        }

        //开启20个线程进行转账
        for (int i = 0; i < NUM_THREADS; i++) {
    
    
            new TransferThread().start();
        }
    }
}

After running the program, a few seconds later, there was a deadlock. The red button of the console was always on, but nothing was printed, indicating that there was a deadlock.
This also shows that even if there are a lot of users, as long as There is a risk of deadlock. Over time, deadlock will occur. Once it occurs, it will cause the system to crash and seriously affect the business.
In business development, the program written to avoid deadlock, this is the most reliable.

Guess you like

Origin blog.csdn.net/qq_33229669/article/details/108481274