Advertisement
Guest User

Untitled

a guest
Oct 20th, 2011
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.71 KB | None | 0 0
  1. class Program {
  2.     public static final int TOTAL_ITERATIONS = 1000000;
  3.  
  4.     private static final int NUM_ACCOUNTS = 2;
  5.  
  6.     public static void main(String[] args) {
  7.         Bank bank = new Bank(NUM_ACCOUNTS);
  8.  
  9.         Thread accountTransfer1 = new Thread(new AccountTransferRunnable(bank));
  10.         Thread accountTransfer2 = new Thread(new AccountTransferRunnable(bank));
  11.  
  12.         accountTransfer1.start();
  13.         accountTransfer2.start();
  14.  
  15.         try {
  16.             accountTransfer1.join();
  17.             accountTransfer2.join();
  18.         } catch (InterruptedException ex) {
  19.             //
  20.         }
  21.     }
  22. }
  23.  
  24. public class Account {
  25.     private int mBalance;
  26.  
  27.     public Account() {
  28.         mBalance = 0;
  29.     }
  30.  
  31.     public int getBalance() {
  32.         return mBalance;
  33.     }
  34.  
  35.     public void withdraw(int value) {
  36.         mBalance -= value;
  37.     }
  38.  
  39.     public void deposit(int value) {
  40.         mBalance += value;
  41.     }
  42. }
  43.  
  44. public class Bank {
  45.     private List<Account> mAccounts;
  46.     private int mSlots;
  47.  
  48.     public Bank(int slots) {
  49.         mAccounts = new ArrayList<Account>(Collections.nCopies(slots, new Account()));
  50.         mSlots = slots;
  51.     }
  52.  
  53.     public int getSlots() {
  54.         return mSlots;
  55.     }
  56.  
  57.     public int getBalance(int id) {
  58.         Account account = mAccounts.get(id);
  59.  
  60.         synchronized(account) {
  61.             return account.getBalance();
  62.         }
  63.     }
  64.  
  65.     public void withdraw(int id, int value) {
  66.         Account account = mAccounts.get(id);
  67.  
  68.         synchronized(account) {
  69.             account.withdraw(value);
  70.         }
  71.     }
  72.  
  73.     public void deposit(int id, int value) {
  74.         Account account = mAccounts.get(id);
  75.  
  76.         synchronized(account) {
  77.             account.deposit(value);
  78.         }
  79.     }
  80.  
  81.     public void transfer(int fromId, int toId, int value) {
  82.         Account fromAccount = mAccounts.get(fromId);
  83.         Account toAccount = mAccounts.get(toId);
  84.  
  85.         synchronized(fromAccount) {
  86.             synchronized(toAccount) {
  87.                 fromAccount.withdraw(value);
  88.                 toAccount.deposit(value);
  89.             }
  90.         }
  91.     }
  92.  
  93. }
  94.  
  95. public class AccountTransferRunnable implements Runnable {
  96.     private final Bank mBank;
  97.  
  98.     public AccountTransferRunnable(Bank bank) {
  99.         mBank = bank;
  100.     }
  101.  
  102.     @Override
  103.     public void run() {
  104.     Random random = new Random();
  105.  
  106.     int slots = mBank.getSlots();
  107.         int fromId, toId;
  108.  
  109.         for(int i = 0; i < Program.TOTAL_ITERATIONS; i++) {
  110.             fromId = random.nextInt(slots);
  111.  
  112.             do {
  113.                 toId = random.nextInt(slots);
  114.             } while(toId == fromId);
  115.  
  116.             mBank.transfer(fromId, toId, 100);
  117.         }
  118.    }
  119. }
  120.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement