多线程-模拟100个账户同时向1个账户存钱

方法一
使用线程池ExecutorService的invokeAll()方法,先将要启动的线程存入集合中,然后一次性启动

public class Account {
    private Double money;

    Account() {
        //定义初始金额 实际从数据库中查询
        money = 0D;
    }

    public synchronized double getMoney() {
        return money;
    }

    //模拟存钱
    public synchronized void despoilment(double num) {
        money += num;
    }
}
class Test{

    public static void main(String[] args) {
        while (true) {
            Account account = new Account();
            testPoolMuti(account);
        }
    }
    //用100个线程同时向同一个账户存钱
    private static void testPoolMuti(Account account) {
        ExecutorService executorService = Executors.newFixedThreadPool(100);
        List<Callable<String>> tasks = new LinkedList<>();
        int i = 100;
        while (i-- > 0) {
            synchronized (account) {
                tasks.add(() -> {
                    account.despoilment(1D);
                    return "当前剩余金额" + account.getMoney();
                });
            }
        }
        try {
            executorService.invokeAll(tasks);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }finally {
            executorService.shutdown();
        }
        System.out.println("Account的账户余额为:" + account.getMoney());
    }
}

输出结果
在这里插入图片描述

性能分析
在这里插入图片描述

方法二
使用线程池+CountDownLatch实现,原理是利用CountDownLatch的await()方法,该方法会让线程等待,直到countDown()的返回值等于0时开始执行

class Test{

    public static void main(String[] args) {
        while (true) {
            Account account = new Account();
            testPool(account);
        }
    }
    //用100个线程同时向同一个账户存钱
    private static void testPool(Account account) {
        ExecutorService executorService = Executors.newFixedThreadPool(100);
        CountDownLatch countDownLatch = new CountDownLatch(100);//线程计数器
        int i = 100;
        while (i-- > 0) {
            ThreadRun run = new ThreadRun(account, countDownLatch);
            executorService.submit(run);
        }
        try {
            countDownLatch.await();
            System.out.println("Account的账户余额为:" + account.getMoney());
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}

class ThreadRun implements Runnable {
    CountDownLatch countDownLatch;
    Account account;

    ThreadRun(Account account) {
        this.account = account;
    }

    ThreadRun(Account account, CountDownLatch countDownLatch) {
        this.account = account;
        this.countDownLatch = countDownLatch;
    }

    @Override
    public synchronized void run() {
        account.despoilment(1D);
        countDownLatch.countDown();
    }
}

输出结果
在这里插入图片描述

性能分析
在这里插入图片描述

总结:
线程池的额invokeAll方法可以让多个线程同时启动,但是只能使用Callable类实现,线程并行执行,同一时刻可以有多个线程处于就绪态,线程总量相对稳定。线程池+线程计数器方法实现更加自由,但是线程串行执行,同一时刻只有一个线程处于就绪态,大量线程同时涌入会导致等待线程过多,且触发gc数量相对较少。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,您的问题是关于Java多线程存钱取钱问题。这是一个经典的多线程同步问题,可以通过使用synchronized关键字来实现线程安全。下面是一个简单的示例代码: ```java public class BankAccount { private int balance; public BankAccount(int balance) { this.balance = balance; } public synchronized void deposit(int amount) { balance += amount; System.out.println("Deposit successful. Balance: " + balance); } public synchronized void withdraw(int amount) { if (balance < amount) { System.out.println("Withdraw failed. Insufficient balance."); return; } balance -= amount; System.out.println("Withdraw successful. Balance: " + balance); } } public class Main { public static void main(String[] args) { BankAccount account = new BankAccount(1000); // Create two threads to simulate deposit and withdraw Thread depositThread = new Thread(() -> { for (int i = 0; i < 5; i++) { account.deposit(100); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } }); Thread withdrawThread = new Thread(() -> { for (int i = 0; i < 5; i++) { account.withdraw(200); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } }); depositThread.start(); withdrawThread.start(); } } ``` 在这个示例代码中,我们创建了一个银行账户类BankAccount,并在其中实现了deposit和withdraw方法,并使用synchronized关键字来保证线程安全。 在main方法中,我们创建了两个线程模拟存款和取款操作,每个线程执行5次操作。我们使用Thread.sleep方法来模拟每个操作之间的间隔,以便更好地观察多线程操作的结果。 当多个线程同时访问BankAccount对象的deposit和withdraw方法时,synchronized关键字可以确保每个方法只能被一个线程访问,从而避免了竞争条件和数据不一致的问题。 希望这个示例代码能够回答您的问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值