前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >多线程转账代码示例

多线程转账代码示例

作者头像
JavaEdge
发布2021-10-18 16:24:18
4420
发布2021-10-18 16:24:18
举报
文章被收录于专栏:JavaEdge
代码语言:javascript
复制
package com.concurrent.test4;

import lombok.extern.slf4j.Slf4j;
import java.util.Random;

@Slf4j(topic = "c.test11:")
/**
 *买票问题
 */
public class Test15 {
    public static void main(String[] args) throws InterruptedException {
        //创建两个账户相互转账,每人开始拥有1000快
        Account a = new Account(1000);
        Account b = new Account(1000);
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 1000; i++) {
                //a给b转账1000次,每次金额随机
                a.transfer(b, randomAmount());
            }
        }, "t1");
        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 1000; i++) {
                //b给a转账1000次,每次金额随机
                b.transfer(a, randomAmount());
            }
        }, "t2");
        t1.start();
        t2.start();
        t1.join();
        t2.join();
		// 查看转账2000次后的总金额
        log.debug("total:{}",(a.getMoney() + b.getMoney()));
    }
    // Random 为线程安全
    static Random random = new Random();
    // 随机 1~100
    public static int randomAmount() {
        return random.nextInt(100) +1;
    }
}
class Account {//账户类
    private int money;//初始化金额
    public Account(int money) {
        this.money = money;
    }
    public int getMoney() {
        return money;
    }
    public void setMoney(int money) {
        this.money = money;
    }
    public synchronized void transfer(Account target, int amount) {
        //如果加synchronized还是不安全的,因为加在方法上等于保护的是自己的共享变量
        //但是此处有两个共享变量this.money和target.money
        //所以这儿应该用synchronized锁住类对象,因为这俩共享变量是一个类中
        synchronized (Account.class){
            if (this.money > amount) {
                this.setMoney(this.getMoney() - amount);
                target.setMoney(target.getMoney() + amount);
            }
        }
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021/08/17 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档