Loading src/main/java/ch15/ryanmonica/RyanAndMonicaUnmodifiableTest.java +8 −4 Changes for src/main/java/ch15/ryanmonica/RyanAndMonicaUnmodifiableTest.java: 8 added lines, 4 removed lines. Original line number Diff line number Diff line package ch15.ryanmonica; import java.util.HashMap; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; // This does not do what you think it does // the original problem is, of course, that the check for balance is separate from the write. That's still true // here, even with an immutable data structure. What you want is a withdrawal method that lets you atomically read // the balance from the map and update it. public class RyanAndMonicaUnmodifiableTest { public static void main(String[] args) throws InterruptedException { long start = System.currentTimeMillis(); Loading Loading @@ -45,7 +48,8 @@ class RyanAndMonicaAccountsJob implements Runnable { class BankAccountCollection { Map<String, Integer> accountBalances = new HashMap<>(); ConcurrentHashMap<String, Integer> accountBalances = new ConcurrentHashMap<>(); { accountBalances.put("checking", 100); accountBalances.put("savings", 500); Loading @@ -60,7 +64,7 @@ class BankAccountCollection { accountBalances.computeIfPresent(accountName, (s, integer) -> integer - amount); } public void makeWithdrawal(int amount, String accountName, String withdrawnBy) { public synchronized void makeWithdrawal(int amount, String accountName, String withdrawnBy) { final Integer balance = accountBalances.get(accountName); if (balance >= amount) { System.out.println(withdrawnBy + " is about to withdraw. balance: " + balance); Loading Loading
src/main/java/ch15/ryanmonica/RyanAndMonicaUnmodifiableTest.java +8 −4 Changes for src/main/java/ch15/ryanmonica/RyanAndMonicaUnmodifiableTest.java: 8 added lines, 4 removed lines. Original line number Diff line number Diff line package ch15.ryanmonica; import java.util.HashMap; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; // This does not do what you think it does // the original problem is, of course, that the check for balance is separate from the write. That's still true // here, even with an immutable data structure. What you want is a withdrawal method that lets you atomically read // the balance from the map and update it. public class RyanAndMonicaUnmodifiableTest { public static void main(String[] args) throws InterruptedException { long start = System.currentTimeMillis(); Loading Loading @@ -45,7 +48,8 @@ class RyanAndMonicaAccountsJob implements Runnable { class BankAccountCollection { Map<String, Integer> accountBalances = new HashMap<>(); ConcurrentHashMap<String, Integer> accountBalances = new ConcurrentHashMap<>(); { accountBalances.put("checking", 100); accountBalances.put("savings", 500); Loading @@ -60,7 +64,7 @@ class BankAccountCollection { accountBalances.computeIfPresent(accountName, (s, integer) -> integer - amount); } public void makeWithdrawal(int amount, String accountName, String withdrawnBy) { public synchronized void makeWithdrawal(int amount, String accountName, String withdrawnBy) { final Integer balance = accountBalances.get(accountName); if (balance >= amount) { System.out.println(withdrawnBy + " is about to withdraw. balance: " + balance); Loading