kingofsocket 2014-07-29 12:01 采纳率: 0%
浏览 227
已采纳

如何使用synchronized使多个线程同步?

有个Bank类, 属性money=0;然后创建三个线程,调用addmoney()往bank里面加钱,
我这样写,为什么不能同步呢 ? 结果不是300万呀?新手请指教.

public class Bank {
private int money=0;

public int getMoney() {
    return money;
}

public void setMoney(int money) {
    this.money = money;
}

public  void addMoney(int money){
    this.money+=money;
}
public void subMoney(int money){
    this.money-=money;
}

}

public class test2 implements Runnable {
private Bank bank;

public test2(Bank bank) {
    this.bank = bank;
}

@Override
public synchronized void run() {
        for (int i = 0; i < 1000; i++) {
            bank.addMoney(1000);
        }
}

public static void main(String[] args) {
    Bank bank = new Bank();
    test2 r1 = new test2(bank);
    test2 r2 = new test2(bank);
    test2 r3 = new test2(bank);
    Thread t1 = new Thread(r1);
    Thread t2 = new Thread(r2);
    Thread t3 = new Thread(r3);
    t1.start();
    t2.start();
    t3.start();
    System.out.println(bank.getMoney());
}

}

展开全部

  • 写回答

7条回答 默认 最新

  • renminzdb2 2014-07-29 15:23
    关注

    如有我现在用同一个runable实例去构造线程,结果就很明显了。
    [code="java"]Thread t1 = new Thread(r1);
    Thread t2 = new Thread(r1);
    Thread t3 = new Thread(r1);
    t1.start();
    t2.start();
    t3.start();
    System.out.println(bank.getMoney());[/code]
    输出:
    [quote]0
    Thread-0:befor:2014-07-29 11:19:34.000034:$0
    Thread-0:after:2014-07-29 11:19:34.000034:$1000
    Thread-0:befor:2014-07-29 11:19:34.000034:$1000
    Thread-0:after:2014-07-29 11:19:34.000034:$2000
    Thread-0:befor:2014-07-29 11:19:34.000034:$2000
    Thread-0:after:2014-07-29 11:19:34.000034:$3000
    Thread-0:befor:2014-07-29 11:19:34.000034:$3000
    Thread-0:after:2014-07-29 11:19:34.000034:$4000
    Thread-0:befor:2014-07-29 11:19:34.000034:$4000
    Thread-0:after:2014-07-29 11:19:34.000034:$5000
    Thread-0:befor:2014-07-29 11:19:34.000034:$5000
    Thread-0:after:2014-07-29 11:19:34.000034:$6000
    Thread-0:befor:2014-07-29 11:19:34.000034:$6000
    Thread-0:after:2014-07-29 11:19:34.000034:$7000
    Thread-0:befor:2014-07-29 11:19:34.000034:$7000
    Thread-0:after:2014-07-29 11:19:34.000034:$8000
    Thread-0:befor:2014-07-29 11:19:34.000034:$8000
    Thread-0:after:2014-07-29 11:19:34.000034:$9000
    Thread-0:befor:2014-07-29 11:19:34.000034:$9000
    Thread-0:after:2014-07-29 11:19:34.000034:$10000
    Thread-2:befor:2014-07-29 11:19:34.000034:$10000
    Thread-2:after:2014-07-29 11:19:34.000034:$11000
    Thread-2:befor:2014-07-29 11:19:34.000034:$11000
    Thread-2:after:2014-07-29 11:19:34.000034:$12000
    Thread-2:befor:2014-07-29 11:19:34.000034:$12000
    Thread-2:after:2014-07-29 11:19:34.000034:$13000
    Thread-2:befor:2014-07-29 11:19:34.000034:$13000
    Thread-2:after:2014-07-29 11:19:34.000034:$14000
    Thread-2:befor:2014-07-29 11:19:34.000034:$14000
    Thread-2:after:2014-07-29 11:19:34.000034:$15000
    Thread-2:befor:2014-07-29 11:19:34.000034:$15000
    Thread-2:after:2014-07-29 11:19:34.000034:$16000
    Thread-2:befor:2014-07-29 11:19:34.000034:$16000
    Thread-2:after:2014-07-29 11:19:34.000034:$17000
    Thread-2:befor:2014-07-29 11:19:34.000034:$17000
    Thread-2:after:2014-07-29 11:19:34.000034:$18000
    Thread-2:befor:2014-07-29 11:19:34.000034:$18000
    Thread-2:after:2014-07-29 11:19:34.000034:$19000
    Thread-2:befor:2014-07-29 11:19:34.000034:$19000
    Thread-2:after:2014-07-29 11:19:34.000034:$20000
    Thread-1:befor:2014-07-29 11:19:34.000034:$20000
    Thread-1:after:2014-07-29 11:19:34.000034:$21000
    Thread-1:befor:2014-07-29 11:19:34.000034:$21000
    Thread-1:after:2014-07-29 11:19:34.000034:$22000
    Thread-1:befor:2014-07-29 11:19:34.000034:$22000
    Thread-1:after:2014-07-29 11:19:34.000034:$23000
    Thread-1:befor:2014-07-29 11:19:34.000034:$23000
    Thread-1:after:2014-07-29 11:19:34.000034:$24000
    Thread-1:befor:2014-07-29 11:19:34.000034:$24000
    Thread-1:after:2014-07-29 11:19:34.000034:$25000
    Thread-1:befor:2014-07-29 11:19:34.000034:$25000
    Thread-1:after:2014-07-29 11:19:34.000034:$26000
    Thread-1:befor:2014-07-29 11:19:34.000034:$26000
    Thread-1:after:2014-07-29 11:19:34.000034:$27000
    Thread-1:befor:2014-07-29 11:19:34.000034:$27000
    Thread-1:after:2014-07-29 11:19:34.000034:$28000
    Thread-1:befor:2014-07-29 11:19:34.000034:$28000
    Thread-1:after:2014-07-29 11:19:34.000034:$29000
    Thread-1:befor:2014-07-29 11:19:34.000034:$29000
    Thread-1:after:2014-07-29 11:19:34.000034:$30000
    [/quote]

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 在ros2的iron版本进行编译时遇到如下问题
  • ¥18 vs用setup project打包项目实现安装完立即运行
  • ¥15 孟德尔随机化TwoSampleMR在线提取结局数据,遇到Error in check_reset(override_429)的问题
  • ¥15 ONNX转RKNN遇到问题
  • ¥60 以太网电缆未接通怎么处理
  • ¥15 关于超声图片进行放射组学的疑问
  • ¥20 已经有功率放大电路图,具体每个元器件的参数怎么算?
  • ¥15 用GIS怎么提取出一个城市的中心城区
  • ¥30 matlab代码调试
  • ¥15 使用ruoyi分离版,下载完成引成功第三方插件报错,报错,换了好多插件都是一样的。
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部