私はJavaを初めて使用しています。私はちょうど "コアJava"の本を読んでいます。私は '条件&ロック'に関する問題に出会った。Javaロック条件待ちと通知:IllegalMonitorStateException
私はいくつかの練習をするために本から日食へのコードを入力しました。
コードを実行すると、 "sufficientFund.wait();"という行が表示されます。 IllegalMonitorStateExceptionをスローします。なぜここに例外がありますか?
私は、このメソッドは、このオブジェクトのモニタの所有者であるスレッドによってのみ呼び出されるべきであることを知っています。私は、現在のスレッドがロックを持っていると思う。なぜなら、 'bankLock.lock();' wait()の直前に実行されます。 私はコードの正しい動作は、現在のスレッドはenoughFund.wait()でハングしている必要がありますが、それはしなかったと思います。
package com.first.java;
import java.util.Scanner;
import java.util.concurrent.locks.*;
public class BankTranf {
private static final int NACCOUNT = 3;
public static final double INITAL_BALANCE = 1000;
public static void main(String[] args) {
Bank bank = new Bank(NACCOUNT, INITAL_BALANCE);
for (int i = 0; i < NACCOUNT; i++) {
TransferRunnable transferRunnable = new TransferRunnable(bank, i, INITAL_BALANCE);
Thread thread = new Thread(transferRunnable);
thread.start();
}
System.out.println("press any key to exit.");
Scanner in = new Scanner(System.in);
in.nextLine();
System.exit(0);
}
}
class Bank {
private final double[] account;
private Lock bankLock;
private Condition sufficientFund;
public Bank(int n, double initialBanlance) {
account = new double[n];
for (int i = 0; i < account.length; i++) {
account[i] = initialBanlance;
}
bankLock = new ReentrantLock();
sufficientFund = bankLock.newCondition();
}
public void transfer(int from, int to, double amount) {
bankLock.lock();
try {
while (account[from] < amount) {
System.out.println(Thread.currentThread().getName() + " does'nt hava enough money");
sufficientFund.wait();
}
System.out.println(Thread.currentThread());
account[from] -= amount;
System.out.printf("%10.2f from %d to %d ", amount, from, to);
account[to] += amount;
System.out.printf(" Total balance: %10.2f%n", getTotalBalance());
sufficientFund.signalAll();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
bankLock.unlock();
}
}
public double getTotalBalance() {
double d = 0;
bankLock.lock();
try {
for (double n : account) {
d += n;
}
return d;
} finally {
bankLock.unlock();
}
}
public int size() {
return account.length;
}
}
class TransferRunnable implements Runnable {
private Bank bank;
private int fromAccount;
private double maxAmount;
private int DELAY = 10;
public TransferRunnable(Bank b, int from, double max) {
bank = b;
this.fromAccount = from;
this.maxAmount = max;
}
@Override
public void run() {
try {
while (true) {
int toAcount = (int) (bank.size() * Math.random());
double amount = maxAmount * Math.random();
bank.transfer(fromAccount, toAcount, amount);
Thread.sleep(4000/* (int)(DELAY*Math.random()) */);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
私はロックとコンディションをすべて削除しました。代わりに "synchronized"を使用する代わりに、コードが期待通りに実行されていました。
public synchronized void transfer(int from, int to, double amount) {
//bankLock.lock();
try {
while (account[from] < amount) {
System.out.println(Thread.currentThread().getName() + " does'nt hava enough money");
wait();
}
System.out.println(Thread.currentThread());
account[from] -= amount;
System.out.printf("%10.2f from %d to %d ", amount, from, to);
account[to] += amount;
System.out.printf(" Total balance: %10.2f%n", getTotalBalance());
notifyAll();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
}
}
public synchronized double getTotalBalance() {
double d = 0;
try {
for (double n : account) {
d += n;
}
return d;
} finally {
}
}
はい、あなたは正しいです! – gfan