package HeadFirstJava;
class BankAccount {
private int balance = 100;
public int getBalance() {
return balance;
}
public void withdraw(int amount) {
balance = balance - amount;
}
}
class BobAndMonica implements Runnable {
private BankAccount account = new BankAccount();
public static void main(String[] args) {
BobAndMonica theJob = new BobAndMonica();
Thread one = new Thread(theJob);
Thread two = new Thread(theJob);
one.setName("Bob");
two.setName("Monica");
one.start();
two.start();
}
@Override
public void run() {
// TODO Auto-generated method stub
for (int x = 0; x < 10; x++) {
makeWithdrawl(10);
}
if (account.getBalance() < 0) {
System.out.println("Overdrawn");
}
}
private void makeWithdrawl(int amount) {
if (account.getBalance() >= amount) {
System.out.println(Thread.currentThread().getName() + " is about to withdraw");
try {
System.out.println(Thread.currentThread().getName() + " is going to sleep");
Thread.sleep(500);
} catch(InterruptedException ex) {ex.printStackTrace();}
System.out.println(Thread.currentThread().getName() + " woke up.");
account.withdraw(amount);
System.out.println(Thread.currentThread().getName() + " completes the withdrawl");
} else {
System.out.println("Sorry, not enough for " + Thread.currentThread().getName());
}
}
}Bob is about to withdrawBob is going to sleep
Monica is about to withdraw
Monica is going to sleep
Monica woke up.
Monica completes the withdrawl
Monica is about to withdraw
Monica is going to sleep
Bob woke up.
Bob completes the withdrawl
Bob is about to withdraw
Bob is going to sleep
Monica woke up.
Monica completes the withdrawl
Monica is about to withdraw
Monica is going to sleep
Bob woke up.
Bob completes the withdrawl
Bob is about to withdraw
Bob is going to sleep
Monica woke up.
Monica completes the withdrawl
Monica is about to withdraw
Monica is going to sleep
Bob woke up.
Bob completes the withdrawl
Bob is about to withdraw
Bob is going to sleep
Monica woke up.
Monica completes the withdrawl
Monica is about to withdraw
Monica is going to sleep
Bob woke up.
Bob completes the withdrawl
Bob is about to withdraw
Bob is going to sleep
Monica woke up.
Monica completes the withdrawl
Monica is about to withdraw
Monica is going to sleep
Bob woke up.
Bob completes the withdrawl
Sorry, not enough for Bob
Sorry, not enough for Bob
Sorry, not enough for Bob
Sorry, not enough for Bob
Sorry, not enough for Bob
Monica woke up.
Monica completes the withdrawl
Sorry, not enough for Monica
Sorry, not enough for Monica
Sorry, not enough for Monica
Sorry, not enough for Monica
OverdrawnBob is about to withdraw
Bob is going to sleep
Monica is about to withdraw
Monica is going to sleep
Monica woke up.
Monica completes the withdrawl
Monica is about to withdraw
Monica is going to sleep
Bob woke up.
Bob completes the withdrawl
Bob is about to withdraw
Bob is going to sleep
Monica woke up.
Monica completes the withdrawl
Monica is about to withdraw
Monica is going to sleep
Bob woke up.
Bob completes the withdrawl
Bob is about to withdraw
Bob is going to sleep
Monica woke up.
Monica completes the withdrawl
Monica is about to withdraw
Monica is going to sleep
Bob woke up.
Bob completes the withdrawl
Bob is about to withdraw
Bob is going to sleep
Monica woke up.
Monica completes the withdrawl
Monica is about to withdraw
Monica is going to sleep
Bob woke up.
Bob completes the withdrawl
Bob is about to withdraw
Bob is going to sleep
Monica woke up.
Monica completes the withdrawl
Monica is about to withdraw
Monica is going to sleep
Bob woke up.
Bob completes the withdrawl
Sorry, not enough for Bob
Sorry, not enough for Bob
Sorry, not enough for Bob
Sorry, not enough for Bob
Sorry, not enough for Bob
Monica woke up.
Monica completes the withdrawl
Sorry, not enough for Monica
Sorry, not enough for Monica
Sorry, not enough for Monica
Sorry, not enough for Monica
Overdrawn
本文通过一个银行账户取款的示例,展示了多线程环境下未使用同步机制可能导致的数据不一致问题。两个线程分别代表Bob和Monica,在进行取款操作时,由于缺乏适当的同步措施,出现了账户透支的情况。

740

被折叠的 条评论
为什么被折叠?



