public static void main(String[] args) { try { Registry registry = LocateRegistry.getRegistry(1099); MonitorInterface monitor = (MonitorInterface) registry.lookup("//localhost/monitor"); Random random = new Random(); int id = monitor.getID(); int max = monitor.getMax(); Bank bank = new Bank(id, max, monitor, random.nextInt(100), registry); registry.bind("//localhost/bank" + id, bank); System.out.println("Bank " + id + " ready! Hit to start..."); System.in.read(); for (int j = 0; j < 1000; j++) { int d = random.nextInt(max - 1); if (d >= id) d++; int q = random.nextInt(10); bank.transfer(q, d); Thread.sleep(random.nextInt(5000)); } } catch (Exception e) { e.printStackTrace(); } }
public synchronized void transfer(int amount, int destiny) throws RemoteException { clock[id]++; monitor.begin(id, destiny, amount, clock); balance -= amount; BankInterface bank = null; try { bank = (BankInterface) registry.lookup("//localhost/bank" + destiny); } catch (Exception e) { e.printStackTrace(); } sync(bank.receive(amount, clock)); clock[id]++; monitor.end(id, destiny, amount, clock); }