有余额2000,两个线程每次取200(同步锁)

package com.a;

/**
 * @author 夜灬瞬
 * @date 2021年12月17日 14:28
 */
public class Test122 extends Thread {
    static Integer money = 2000;

    public Test122(String name) {
        super(name);
    }

    @Override
    public void run() {
        boolean flag = true;
        while (flag) {
            synchronized (this) {
                if (money > 0) {
                    System.out.println(this.getName() + "取200元,银行二余额" + (money -= 200));
                } else {
                    flag = false;
                }
            }
        }
    }

}

#学习路径#
全部评论
3 回复 分享
发布于 2021-12-20 17:52
这好像有问题吧,虽然你锁了this,但新建两个线程显然是不同的this,你确定没有问题吗
2 回复 分享
发布于 2021-12-20 11:17
1 回复 分享
发布于 2021-12-20 11:26

相关推荐

玉无心❤️:发照片干啥 发简历啊
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
3
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务