wo317458719 2016-03-22 15:10 采纳率: 0%
浏览 1255

多线程同步问题,请高手帮忙分析一下原因

先上代码

public class Banck {
private Double[] accounts;
public Banck(int n,Double inintPrice) {
    accounts= new Double[n];
    for(int i=0;i<n;i++){
        accounts[i]=inintPrice;
    }
}


public  void transfer(Integer from,Integer to,Double mouny) throws InterruptedException{
    synchronized(accounts){
    while(accounts[from]<mouny){
        return;
    }
    System.out.print("线程"+Thread.currentThread());
    accounts[from]-=mouny;
    System.out.printf(" 金额 %10.2f 从账户  %d 到  %d",mouny,from,to);
    accounts[to]+=mouny;
    System.out.printf(" 总金额 %10.2f",getTotal());
    System.out.println();
    }
}

public  Double getTotal() throws InterruptedException{
    double total=0d;
    synchronized(accounts){
    for( int i=0;i<accounts.length;i++){
        total+=accounts[i];
    }
    }
    return total;
}
public int size(){
    return accounts.length;
}
public Double[] getAccounts() {
    return accounts;
}

public void setAccounts(Double[] accounts) {
    this.accounts = accounts;
}

}

#############################
public class TransferRunnable implements Runnable{
private Banck banck;
private Double amount;
private int fromBank;
public TransferRunnable(Banck bank,int from , Double amount){
this.banck=bank;
this.fromBank=from;
this.amount=amount;
}
@Override
public void run() {
synchronized(this){
while(true){
int toBanck=(int)(banck.size()*Math.random());
Double mouny=amount*Math.random();
try {
banck.transfer(fromBank, toBanck, mouny);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
System.out.println("---"+banck.getTotal());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public Banck getBanck() {
return banck;
}
public void setBanck(Banck banck) {
this.banck = banck;
}
public int getFromBank() {
return fromBank;
}
public void setFromBank(int fromBank) {
this.fromBank = fromBank;
}
public Double getAmount() {
return amount;
}
public void setAmount(Double amount) {
this.amount = amount;
}

}

############################################
public class ThreadTest {

public static void main(String[] args) {
    Banck b=new Banck(10,100d);
    for(int i=0;i<b.size();i++){
        TransferRunnable tr=new TransferRunnable(b,i,100d);
        Thread t=new Thread(tr);
        t.start();
    }
}

}

运行结果:
线程Thread[Thread-3,5,main] 金额 62.13 从账户 3 到 4 总金额 1000.00
---1000.0
线程Thread[Thread-4,5,main] 金额 13.78 从账户 4 到 2 总金额 1000.00
---1000.0
线程Thread[Thread-5,5,main] 金额 6.86 从账户 5 到 5 总金额 1000.00
---1000.0
线程Thread[Thread-9,5,main] 金额 15.11 从账户 9 到 6 总金额 1000.00
---1000.0
线程Thread[Thread-1,5,main] 金额 4.90 从账户 1 到 3 总金额 1000.00
---1000.0000000000001
线程Thread[Thread-8,5,main] 金额 46.37 从账户 8 到 0 总金额 1000.00
---1000.0
---1000.0
线程Thread[Thread-0,5,main] 金额 50.18 从账户 0 到 3 总金额 1000.00
---1000.0
线程Thread[Thread-6,5,main] 金额 19.87 从账户 6 到 7 总金额 1000.00
---999.9999999999999
线程Thread[Thread-7,5,main] 金额 1.95 从账户 7 到 7 总金额 1000.00
---999.9999999999999
线程Thread[Thread-3,5,main] 金额 34.37 从账户 3 到 8 总金额 1000.00
---999.9999999999999
---999.9999999999999
线程Thread[Thread-5,5,main] 金额 84.46 从账户 5 到 3 总金额 1000.00

我想知道,在run方法里面调用Banck类的getTotal()方法,为什么会变。我加了同步锁为什么不启作用?该怎么解决?

  • 写回答

2条回答 默认 最新

  • devmiao 2016-03-22 15:56
    关注

    同步的调用是不是有问题,调试下。

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog