Advertisement
STANAANDREY

aclabs ex threads java

Mar 8th, 2023 (edited)
1,006
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.53 KB | None | 0 0
  1. import java.util.HashMap;
  2. import java.util.Random;
  3. import java.util.Scanner;
  4. import java.util.concurrent.ExecutorService;
  5. import java.util.concurrent.Executors;
  6. import java.util.concurrent.atomic.AtomicReference;
  7.  
  8. public class Main {
  9.  
  10.     AtomicReference<HashMap<String, Integer>> accToAmount = new AtomicReference<>(new HashMap<>());
  11.     boolean withdraw(String accountNumber, Integer amountToSubstract) {
  12.         if (accToAmount.get().get(accountNumber) < amountToSubstract) {
  13.             return false;
  14.         }
  15.  
  16.         HashMap<String, Integer> currentMap, newMap;
  17.         synchronized (this){
  18.             currentMap = accToAmount.get();
  19.             newMap = new HashMap<>(currentMap);
  20.             newMap.put(accountNumber, currentMap.get(accountNumber) - amountToSubstract);
  21.             accToAmount.set(newMap);
  22.         };
  23.  
  24.         return true;
  25.     }
  26.  
  27.     void add(String accountNumber, Integer amountToAdd) {
  28.         HashMap<String, Integer> currentMap, newMap;
  29.         synchronized (this){
  30.             currentMap = accToAmount.get();
  31.             newMap = new HashMap<>(currentMap);
  32.             newMap.put(accountNumber, currentMap.get(accountNumber) - amountToAdd);
  33.             accToAmount.set(newMap);
  34.         };
  35.     }
  36.  
  37.     public static void main(String[] args) {
  38.         Main main = new Main();
  39.         for (int i = 1; i <= 10; i++) {
  40.             main.accToAmount.get().put("Account" + i, (int)1e6);
  41.         }
  42.  
  43.         final int NUM_THREADS = 100;
  44.         ExecutorService executorService = Executors.newFixedThreadPool(NUM_THREADS);
  45.         for (int i = 0; i < NUM_THREADS; i++) {
  46.             executorService.execute(() -> {
  47.                 //main.withdraw("Account1", 100);
  48.                 Random random = new Random();
  49.                 int acNr = random.nextInt(10) + 1;
  50.                 for (int j = 0; j < 10; j++) {
  51.                     double op = random.nextDouble();
  52.                     int val = random.nextInt(1000) + 1;
  53.                     if (op >= 0.5) {
  54.                         boolean ok = main.withdraw("Account" + acNr, val);
  55.                         if (!ok) {
  56.                             System.err.println("failed withdraw");
  57.                         }
  58.                     } else {
  59.                         main.add("Account" + acNr, val);
  60.                     }
  61.                 }
  62.  
  63.             });
  64.         }
  65.         executorService.shutdown();
  66.  
  67.         for (int i = 1; i <= 10; i++) {
  68.             System.out.println("Account" + i + ": " + main.accToAmount.get().get("Account" + i));
  69.         }
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement