*/
public static void main(String[] args) {
Thread t = new Thread(new Bank());
Thread t1 = new Thread(new Bank());
Thread t2 = new Thread(new Bank());
t.setName("用户A");
t1.setName("用户B");
t2.setName("用户C");
t.start();
t1.start();
t2.start();
}
}
class Bank implements Runnable{
private static int money = 100;
private static int i = 1;
@Override
public void run() {
for (; i < 20; i++) {
synchronized (Bank.class) {
if ("用户A".equals(Thread.currentThread().getName())) {
setMoney(money+100);
System.out.println("用户A线程正在执行第"+i+"次,增加了100元,目前money的值为"+money+"元");
}
else if ("用户B".equals(Thread.currentThread().getName())) {
Random r = new Random();
int nextInt = r.nextInt(100);
setMoney(money+nextInt);
System.out.println("用户B线程正在执行第"+i+"次,增加了"+nextInt+"元,目前money的值为"+money+"元");
}