DemoBankAccount.java
- package ch.hslu.exercises.sw05.input.ex2;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import java.util.List;
- import java.util.stream.Collectors;
- import java.util.stream.IntStream;
- public class DemoBankAccount {
- private static final int number = 1_000;
- private static final int amount = 5_000_000;
- private static final List<BankAccount> sourceBankAccounts = IntStream.range(0, number)
- .mapToObj(x -> new BankAccount(amount))
- .collect(Collectors.toList());
- private static final List<BankAccount> targetBankAccounts = IntStream.range(0, number)
- .mapToObj(x -> new BankAccount())
- .collect(Collectors.toList());
- private static final Logger LOG = LoggerFactory.getLogger(DemoBankAccount.class);
- public static void main(String[] args) {
- final Thread[] threads = new Thread[number * 2];
- for (int i = 0; i < number; i++) {
- threads[i] = new Thread(new AccountTask(sourceBankAccounts.get(i), targetBankAccounts.get(i), amount));
- threads[i + number] = new Thread(new AccountTask(targetBankAccounts.get(i), sourceBankAccounts.get(i), amount));
- }
- for (final Thread thread : threads) {
- thread.start();
- }
- for (final Thread thread : threads) {
- try {
- thread.join();
- } catch (InterruptedException e) {
- return;
- }
- }
- LOG.info("Bank accounts after transfers");
- for (int i = 0; i < number; i++) {
- LOG.info("source({}) = {}; target({}) = {};", i, sourceBankAccounts.get(i).getBalance(), i, targetBankAccounts.get(i).getBalance());
- }
- }
- }