题目如下:模拟银行转账业务, 有10个账户,每个账户初始金额为10000,有10个线程随机选择两个账户进行转账,每次转的金额是1000以内的. 每个线程转100次. 最后输出10个账户的总金额。
代码分为三个模块:主程序、线程和单个银行账户;
package com.gcs.AccountTest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;
public class Main {
public static void main(String[] args) {
List<Account> accountList = new ArrayList<Account>();
Map<Account, ReentrantLock>accLock = new HashMap<Account, ReentrantLock>();
//10个账户;
Account acc = null;
for (int i = 0; i < 10; i++) {
acc = new Account(String.valueOf(i+1));
accountList.add(acc);
accLock.put(acc, new ReentrantLock());
}
//10个线程;
List<Thread> threadList = new ArrayList<Thread>();
for (int i = 0; i < 10; i++) {
threadList.add(new AccountThread(accountLi