









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










{ public class MyClass { public synchronized Object getNextThing()private List
// Thread sleeps until some other Thread calls notify()
catch (InterruptedException ie) { }}
return this.l.remove(0);}
{ public synchronized void putNextThing(Object o)} this.notify(); // Will "wake" thread waiting for data, if there is anythis.l.add(o);
**{ public class BankAccount
*on the enclosing BankAccount instance /
{ public int getNumber()private double balance; return this.number;
{ public synchronized double getBalance()} return this.balance;
{ public synchronized void setBalance(double balance)} this.balance = balance;
{ public class BankAccount public static transfer(Account fromAccount, Account toAccount, double amount)/ This method is static because it affects two instances./** throws OverdraftException**
if (amount <= 0.0) throw new IllegalArgumentException("Transfer amount must be positive.");
if (newBalance < 0.0)double newBalance = fromBalance - amount;double fromBalance = fromAccount.getBalance(); throw new OverdraftException(newBalance);
toAccount.setBalance(toAccount.getBalance() + amount);fromAccount.setBalance(newBalance);
The
transfer()
(^) method
performs 3 operations:has a race condition. It
fi cient balance
in 1
account^
account
account
Making the accessors and mutators
(^) synchronized
(^) prevents corruption of a
single object, but it doesn’t help coordinate multiple objects. The
(^) static
transfer()
(^) method also acquires and releases monitors as it enters and exits
synchronized
(^) instance members, leaving gaps where another thread can
alter the object. The
(^) static
(^) method needs to hold the monitors for both
objects for the entire time it’s executing...
acct 1 has $
yes
Preemption
acct 1 has $
(^) yes
set acct 2 to $30set acct 1 to $
Preemption
set acct 3 to $30 set acct 1 to $
{ public class BankAccount public static transfer(Account fromAccount, Account toAccount, double amount)/ This method is static because it affects two instances./** throws OverdraftException**
if (amount <= 0.0) throw new IllegalArgumentException("Transfer amount must be positive.");
{ synchronized (fromAccount) { synchronized (toAccount) if (newBalance < 0.0)double newBalance = fromBalance - amount;double fromBalance = fromAccount.getBalance(); throw new OverdraftException(newBalance);
toAccount.setBalance(toAccount.getBalance() + amount);fromAccount.setBalance(newBalance);
times (when A thread that already holds a monitor can reacquire the same monitor multiple }
(^) methods are called inside the
(^) block).
{ public class BankAccount public static transfer(Account fromAccount, Account toAccount, double amount) throws OverdraftException
if (amount <= 0.0) throw new IllegalArgumentException("Transfer amount must be positive.");
if (fromAccount.getNumber() < toAccount.getNumber())Account first, second; { first = fromAccount; second = toAccount; }
else { first = toAccount; second = fromAccount; }
{ synchronized (first) { synchronized (second) if (newBalance < 0.0)double newBalance = fromBalance - amount;double fromBalance = fromAccount.getBalance(); throw new OverdraftException(newBalance);
toAccount.setBalance(toAccount.getBalance() + amount);fromAccount.setBalance(newBalance);
{ public class BankAccount public static transfer(Account fromAccount, Account toAccount, double amount)/ This method is static because it affects two instances./** throws OverdraftException**
if (amount <= 0.0) throw new IllegalArgumentException("Transfer amount must be positive.");
toAccount.transaction(amount);fromAccount.transaction(-amount);