@Test public void testCreateAccountsByMultipleThreadsConcurrently() throws InterruptedException { int nWorkers = 7; CountDownLatch latcher = new CountDownLatch(nWorkers); List<Integer> createdAccountIdList = new CopyOnWriteArrayList<>(); Runnable newAccountTask = () -> { Account account = new Account(new Client("Vali", "Valiev"), "RUR", 1_000_000.0); int accountId = accountDao.createAccount(account).getId(); createdAccountIdList.add(accountId); latcher.countDown(); }; ExecutorService threadPool = Executors.newFixedThreadPool(nWorkers); for (int i = 0; i < nWorkers; i++) threadPool.submit(newAccountTask); latcher.await(); List<Integer> expectedIds = Arrays.asList(1, 2, 3, 4, 5, 6, 7); Collections.sort(createdAccountIdList); assertArrayEquals(expectedIds.toArray(), createdAccountIdList.toArray()); }
@Override public Account createAccount(Account account) { return db.computeIfAbsent(idGenerator.incrementAndGet(), id -> { account.setId(id); return account; }); }
InMemoryAccountDaoImpl implements AccountDao { @Override public Account createAccount(Account account) { return db.computeIfAbsent(idGenerator.incrementAndGet(), id -> { account.setId(id); return account; }); } }
InMemoryAccountDaoImpl implements AccountDao { @Override public Account createAccount(Account account) { return db.computeIfAbsent(idGenerator.incrementAndGet(), id -> { account.setId(id); return account; }); }  }
InMemoryAccountDaoImpl implements AccountDao { @Override public Account createAccount(Account account) { return db.computeIfAbsent(idGenerator.incrementAndGet(), id -> { account.setId(id); return account; }); }  @Override Account createAccount(Account account); @Override Account findAccountById(int accountId); @Override List<Account> getAllAccounts(); @Override List<Account> getAllAccountsOf(int size, String sortBy); @Override Transaction updateBalance(Transaction tx); }
InMemoryAccountDaoImpl implements AccountDao { @Override public Account createAccount(Account account) { return db.computeIfAbsent(idGenerator.incrementAndGet(), id -> { account.setId(id); return account; }); }  @Override Account createAccount(Account account); @Override Account findAccountById(int accountId); @Override List<Account> getAllAccounts(); @Override List<Account> getAllAccountsOf(int size, String sortBy); @Override Transaction updateBalance(Transaction tx);  }