![]() |
#2
rind2019-03-22 14:02
|
服务端程序

import java.util.Arrays;
/**
* a Bank with a number of bank accounts that uses Synchronization primitives
* @author john
*
*/
public class BankSynch {
private final double[] accounts;
/**
* Construct the bank
* @param n the number of accounts
* @param initialBlance the initial balance for each account
*/
public BankSynch(int n,double initialBalance) {
accounts = new double[n];
Arrays.fill(accounts, initialBalance);
}
/**
* Transfer the money from one account to another
* @param from the account transfer from
* @param to the account transfer to
* @param amount the amount to transfer
* @throws InterruptedException
*/
public synchronized void transfer(int from, int to, double amount) throws InterruptedException {
while(accounts[from] < amount) {
wait();
System.out.println(Thread.currentThread());
accounts[from] -= amount;
System.out.printf("%10.2f from %d to %d",amount,from,to);
accounts[to] += amount;
System.out.printf("Total balance: %10.2f%n",getTotalBalance());
notifyAll();
}
}
/**
* Get the sum of all accounts balance
* @return the total balance
*/
private Object getTotalBalance() {
double sum = 0;
for(double a : accounts) {
sum += a;
}
return sum;
}
/**
* Gets the number of accounts in the bank
* @return the number of accounts
*/
public int size() {
return accounts.length;
}
}
客户端程序/**
* a Bank with a number of bank accounts that uses Synchronization primitives
* @author john
*
*/
public class BankSynch {
private final double[] accounts;
/**
* Construct the bank
* @param n the number of accounts
* @param initialBlance the initial balance for each account
*/
public BankSynch(int n,double initialBalance) {
accounts = new double[n];
Arrays.fill(accounts, initialBalance);
}
/**
* Transfer the money from one account to another
* @param from the account transfer from
* @param to the account transfer to
* @param amount the amount to transfer
* @throws InterruptedException
*/
public synchronized void transfer(int from, int to, double amount) throws InterruptedException {
while(accounts[from] < amount) {
wait();
System.out.println(Thread.currentThread());
accounts[from] -= amount;
System.out.printf("%10.2f from %d to %d",amount,from,to);
accounts[to] += amount;
System.out.printf("Total balance: %10.2f%n",getTotalBalance());
notifyAll();
}
}
/**
* Get the sum of all accounts balance
* @return the total balance
*/
private Object getTotalBalance() {
double sum = 0;
for(double a : accounts) {
sum += a;
}
return sum;
}
/**
* Gets the number of accounts in the bank
* @return the number of accounts
*/
public int size() {
return accounts.length;
}
}

/**
* This program shows data corruption when multiple thread access the dataStructure
* @author john
*
*/
public class UnsychBankAccountTest {
public static final int NACCOUNTS =100;
public static final double INITIAL_BALANCE =1000;
public static final double MAX_AMOUNT = 1000;
public static final int DELAY= 10;
public static void main(String[] args) {
BankSynch bank = new BankSynch(NACCOUNTS,INITIAL_BALANCE);
for(int i =0;i<NACCOUNTS;i++) {
int fromAccount = i;
Runnable r = () ->{
try {
while(true) {
int toAccount = (int)(bank.size()*Math.random());
double amount = MAX_AMOUNT * Math.random();
bank.transfer(fromAccount, toAccount, amount);
Thread.sleep((int)(DELAY*Math.random()));
}
} catch (InterruptedException e) {
}
};
Thread t = new Thread(r);
t.start();
}
}
}
* This program shows data corruption when multiple thread access the dataStructure
* @author john
*
*/
public class UnsychBankAccountTest {
public static final int NACCOUNTS =100;
public static final double INITIAL_BALANCE =1000;
public static final double MAX_AMOUNT = 1000;
public static final int DELAY= 10;
public static void main(String[] args) {
BankSynch bank = new BankSynch(NACCOUNTS,INITIAL_BALANCE);
for(int i =0;i<NACCOUNTS;i++) {
int fromAccount = i;
Runnable r = () ->{
try {
while(true) {
int toAccount = (int)(bank.size()*Math.random());
double amount = MAX_AMOUNT * Math.random();
bank.transfer(fromAccount, toAccount, amount);
Thread.sleep((int)(DELAY*Math.random()));
}
} catch (InterruptedException e) {
}
};
Thread t = new Thread(r);
t.start();
}
}
}