Ich habe Probleme mit zwei Threads, die nicht richtig zu synchronisieren scheinen. Ich habe grundsätzlich einen booleschen Wertnamen "belegt". Es wird auf false gesetzt, wenn kein Thread gestartet wird. aber wenn man startet, sind die Thread-Sets belegt. Ich habe eine Klasse, die den Thread (run) hat und sie ruft die unten stehenden Funktionen auf.Multi-Thread Producer/Consumer Synchronisationsprobleme
Dies ist ein Scheinbankbeispiel, das einen Betrag (Anfangssaldo) aufnimmt und dann Abhebungen und Einzahlungen nach dem Zufallsprinzip ausführt. Mein Professor Erwähnung etwas über Signalisierung bilden den Rückzug Faden zum Pfand Gewinde? Wie funktioniert das? Der Rückzugfaden sollte laufen, bis das Guthaben zwei niedrig ist und auf einen Pfandfaden warten. Wie soll ich das machen?
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();
}
}
Probieren Sie eine BlockingQueue aus. –