import java util concurrent Callable Created with IntelliJ IDEA User d

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import java.util.concurrent.Callable;
/**
* Created with IntelliJ IDEA.
* User: dmytro.zaytsev
* Date: 4/1/14
* Time: 11:33 PM
* To change this template use File | Settings | File Templates.
*/
public class Transaction implements Callable<Boolean> {
BankAccount from;
BankAccount to;
int amount;
int id;
public Transaction(BankAccount from, BankAccount to, int amount, int id) {
this.amount = amount;
this.from = from;
this.to = to;
this.id = id;
}
@Override
public Boolean call() throws InsufficientFundsException {
try {
Thread.sleep((int)(Math.random()+0.5)*3000);
} catch (InterruptedException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
System.out.println("Execution transaction " + id+"\nTransfering from " + from.id + " account to " + to.id + " one");
from.withdraw(amount);
to.deposit(amount);
System.out.println("Balance on " + from.id + " is " + from.currentBalance);
if (from.currentBalance > to.currentBalance) return true;
else return false;
}
}