Java 我的同步不起作用

Java 我的同步不起作用,java,synchronization,Java,Synchronization,我尝试在Bank.transfer中使用synchronized和ReentrantLock,但得到的输出如下: 从7岁到3岁转为82.0岁。总计918人,从0人到4人,共计27.0人。总数973 而总数必须等于1000。告诉我我怎么了 public class expr { public static Bank b = new Bank(); public static void main(String[] args) throws IOException, Interrupt

我尝试在Bank.transfer中使用synchronized和ReentrantLock,但得到的输出如下:

从7岁到3岁转为82.0岁。总计918人,从0人到4人,共计27.0人。总数973

而总数必须等于1000。告诉我我怎么了

public class expr {
    public static Bank b = new Bank();
    public static void main(String[] args) throws IOException, InterruptedException {
        for (int i = 0; i < 4; i++) {
            new BankTransfer();
        }
    }
}

public class BankTransfer implements Runnable{

    public BankTransfer() {
        Thread t = new Thread(this);
        t.start();
    }

    @Override
    public void run() {

        while (true){
            int from = (int) (expr.b.size * Math.random());
            int to = (int) (expr.b.size * Math.random());
            int amount = (int) (100 * Math.random());
            expr.b.transfer(from, to, amount);

            try {
                Thread.sleep((long) (2000 * Math.random()));
            } catch (InterruptedException e) {
                System.out.println("Thread was interrupted!");
                return;
            }

        }
    }


}

public class Bank {
    private int[] accounts;
    public int size = 10;
    private Lock block = new ReentrantLock();
    public boolean transfer(int from, int to, double amount){
        block.lock();
        try{
            if(accounts[from] >= amount && from != to){
                accounts[from] -= amount;
                System.out.println("From " + from + " to " + to + " transfered " + amount + ". Total " + getTotal());
                accounts[to] += amount;
                return true;
            }
        }finally {
            block.unlock();
        }
        return false;
    }
    public Bank(){
        accounts = new int[size];
        for (int i = 0; i < size; ++i) {
            accounts[i] = 100;
        }
    }
    private int getTotal(){
        int sum = 0;
        for (int i = 0; i < size; ++i) sum += accounts[i];
        return sum;
    }
}

在完成变压器两端后,计算总流量。。。i、 e.将System.println移到accounts[到]+=amount.

在完成transfier的两端后计算总数。。。i、 e.将System.println移到accounts[到]+=金额之后。

这部分看起来很奇怪:

accounts[from] -= amount;
System.out.println("From " + from + " to " + to + " transfered " + amount + ". Total " + getTotal());
accounts[to] += amount;
您正在传输完成前打印总数。

此部分看起来很奇怪:

accounts[from] -= amount;
System.out.println("From " + from + " to " + to + " transfered " + amount + ". Total " + getTotal());
accounts[to] += amount;

您正在打印转账完成前的总额。

您在从一个帐户扣除款项后,但在将其添加到另一个帐户之前,调用getTotal。根据转账金额,它将始终显示小于100。

您在从一个帐户扣除款项后,但在将其添加到另一个帐户之前,调用getTotal。根据传输的金额,它将始终显示小于100。

选择未中断。记住这句话,意思是,如果一种成熟语言的一个基本部分不能正常工作,那可能就是你正在做的事情。看看你打印输出的地方。我会觉得这是一个奇怪的“巧合”,转移的金额和总数达到1000;选择没有被破坏。记住这句话,意思是,如果一种成熟语言的一个基本部分不能正常工作,那可能就是你正在做的事情。看看你打印输出的地方。我会觉得这是一个奇怪的“巧合”,转移的金额和总数达到1000;