我需要有关使用并发的 java 分配的帮助。我遇到的问题出在get
方法上,我找不到任何问题。但是,感觉它没有被正确访问,或者它没有做它应该做的事情。总而言之,问题是我得到了所有的金属,但我没有给消费者任何东西,我不知道这是怎么回事。如果我需要提供任何其他信息,请告诉我。
每个经纪人都持有所有三种金属的库存,但只是其中一种金属的“供应商”,称为“专业”。精炼厂不时将一批精炼金属交付给作为其供应商的经纪人。例如,精炼厂可能向黄金供应商提供 30 盎司黄金。消费者定期向经纪人下订单。每个订单指定每种金属的盎司数。它可以与任何经纪人一起放置。如果可能,经纪人将从自己的股票中完成订单。如果金属 M 的供应商因为手头没有足够的 M 而无法完成订单,它就会等到从精炼商那里得到更多。但是,如果它做空了某种其他金属,它会试图通过与该金属的供应商进行交易来获得它。为了简单起见,我们会假设,有点不切实际,盎司的黄金、铂金或铀都具有同等价值。也就是说,三盎司黄金可以换成三盎司铀或三盎司铂金。
抱歉,我无法显示使用 BrokerImplementation 的类。我不能,因为它们都是 .class 文件,并且认为上传位代码没有任何帮助。
提前感谢您提供的任何帮助。
// This class overrides all it's methods from the Broker interface
public class BrokerImplementation implements Broker, IBM {
int specialty;
int[] metals = {0, 0, 0};
/**
* The constructor takes a single integer parameter, the code for the metal
* which this broker supplies.
*
* @param specialty
*/
public BrokerImplementation(int specialty) {
this.specialty = specialty;
}
/**
* This method is used by Project2.main to audit the global state when the
* system shuts down. The Broker should fill in result with the amount of
* each metal it has on hand.
*
* @param result
*/
@Override
public void getAmountOnHand(int[] result) {
//GOLD, PLATINUM, URANIUM are are constants in the IBM interface
//which correspond to the indexes {0, 1, 2}
result[GOLD] = metals[GOLD];
result[PLATINUM] = metals[PLATINUM];
result[URANIUM] = metals[URANIUM];
}
/**
* A consumer calls this method to place an order. The argument is a
* three-element array indicating the number of ounces of gold, platinum,
* and uranium desired. It should return only when the order has been
* filled.
*
* @param metals
*/
@Override
public void get(int[] order) {
for(int i = 0; i < 3; i++){
if (metals[i] > order[i]) {
metals[i] -= order[i];
} else {
this.swap(i, order[i] - metals[i]);
this.get(order);
try {
wait();
} catch (InterruptedException ex) {
Logger.getLogger(BrokerImplementation.class.getName()).log(Level.SEVERE, null, ex);
}
notifyAll();
}
}
}
/**
* Another broker calls this method to swap one metal for another. The what
* argument indicates one of the metals; the other one is the metal in which
* this broker specializes. The ounces argument indicates how many ounces to
* swap.
*
* @param what
* @param ounces
*/
@Override
public void swap(int what, int ounces) {
synchronized (this) {
if (metals[specialty] >= ounces) {
metals[specialty] -= ounces;
metals[what] += ounces;
} else {
notifyAll();
try {
wait();
} catch (InterruptedException ex) {
Logger.getLogger(BrokerImplementation.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
/**
* The refiner calls this method to deliver a load of metal to the broker.
* The argument ounces is a number of ounces. The metal is the one this
* broker supplies.
*
* @param ounces
*/
@Override
public void deliver(final int ounces) {
System.out.println("available " + metals[specialty]);
metals[specialty] += ounces;
}