1.什么是竞争条件？

2.我们下面以一个我们之前提到过的银行转账的例子为例来说明这个问题。

```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];
}
}

public void transfer(int fromAccount, int toAccount, double money) {
if (accounts[fromAccount] < money) {
return;
}
accounts[fromAccount] -= money;
System.out.printf("从" + fromAccount + "账户转出%10.2f元,", money);
accounts[toAccount] += money;
System.out.printf("从" + toAccount + "账户转入%10.2f元,", money);
System.out.printf("总数：%10.2f元", 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;
}

@Override
public 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 < 20; i++) {
}
}
}
```

......