正しく同期されていない2つのスレッドに関する問題が発生しています。私は基本的にブール値の名前は "占領"を持っています。スレッドが起動していない場合はfalseに設定されます。しかし、スレッドが起動すると、占有されているスレッドセットは真です。スレッド(実行)を持つクラスがあり、以下の関数を呼び出します。マルチスレッドプロデューサ/コンシューマ同期の問題
これは金額(初期残高)を取り込んで引き出しと預金をランダムに実行するモックバンクの例です。私の教授は、預金スレッドに退会スレッドを通知することについて何か言及していますか?それはどのように機能するのですか?引き出しのスレッドは、残高が2になるまで実行し、入金スレッドを待ちます。それはどうしたらいいですか?
package bank;
import java.util.Random;
import bank.Bank;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.Condition;
/**
*
* @author KJ4CC
*/
public class Action {
private Lock accessLock = new ReentrantLock();
private Condition cond = accessLock.newCondition();
//private Condition withdraw = accessLock.newCondition();
Random rand = new Random();
Object lock = new Object();
Bank getBalance = new Bank();
public void widthdrawl(int threadNum) throws InterruptedException {
int amount = rand.nextInt(50);
accessLock.lock();
if (getBalance.getbalance() > amount) {
getBalance.setBalance(getBalance.getbalance() - amount);
System.out.println("\t\t\tThread " + threadNum + " withdrawls " + amount + "\t Balance is " + getBalance.getbalance());
} else {
System.out.println("\t\t\tThread " + threadNum + " Failed to withdrawl " + amount + "\t Balance is " + getBalance.getbalance());
cond.await();
}
accessLock.unlock();
Thread.sleep(rand.nextInt(5));
}
public void deposit(int threadNum) throws InterruptedException {
int amount = rand.nextInt(200);
accessLock.lock();
getBalance.setBalance(getBalance.getbalance() + amount);
System.out.println("Thread " + threadNum + " Deposits " + amount + "\t\t\t\t Balance is " + getBalance.getbalance());
Thread.sleep(rand.nextInt(100));
cond.signal();
accessLock.unlock();
}
}
BlockingQueueをチェックアウトしてみてください。 –