1

マルチスレッドの銀行振込の問題を試しています。ただし、スレッドを正しく同期することに問題があります。スレッドがアカウント間で送金を実行した後、定期的にテストして、お金がまったく得られなかったり、まったく失われたりしていないことを確認します。

スレッドがテスト メソッドに入ると、他のスレッドが転送メソッドに入るのを防ぐフラグを設定し、現在転送を実行しているすべてのスレッドが終了するまで待機する必要があります。

public void transfer(int from, int to, int amount) {        
    //finish running all threads' current transactions before test

    accounts[from].waitForAvailableFunds(amount);
    if (!open) return;    

    //checks to see if any thread is currently testing
    //if so, wait
    while(flag) {
        try {
            wait();
        } catch (InterruptedException e) { /*ignore*/ }
    }

    //do not execute these two statements when a thread
    //is performing a test
    if (accounts[from].withdraw(amount)) {
        accounts[to].deposit(amount);

    }  

    if (shouldTest() && !flag)   test();

}

//only one thread can perform a test at any given moment
public synchronized void test() {        

    //when test starts set a flag telling threads to
    //not begin any new transfers
    flag = true;

    //wait for all threads currently performing transfers
    //to finish current transfer before continuing


    int sum = 0;
    for (int i = 0; i < accounts.length; i++) {
        System.out.printf("%s %s%n", 
                Thread.currentThread().toString(),accounts[i].toString());
        sum += accounts[i].getBalance();
    }
    System.out.println(Thread.currentThread().toString() + 
            " Sum: " + sum);
    if (sum != numAccounts * initialBalance) {
        System.out.println(Thread.currentThread().toString() + 
                " Money was gained or lost");
        System.exit(1);
    } else {
        System.out.println(Thread.currentThread().toString() + 
                " The bank is in balance");
    }

    //reset flag and notify threads test is complete.
    flag = false;
    notifyAll();
}

まず、フラグを設定して正しく待機しているとは確信していません。次に、テスト メソッドに入ったスレッドが、現在の転送を完了するために既に転送を実行している他のすべてのスレッドを待機させるにはどうすればよいですか。

ありがとう

4

1 に答える 1

0

volatile を使用してフラグを装飾し、別のスレッドで変更されたときに影響を受けるようにする必要があると思います。

于 2012-10-02T02:41:37.923 に答える