Note that there are some explanatory texts on larger screens.

plurals
  1. POjava concurrency, producer(broker) and consumer
    text
    copied!<p>I need help with a java assignment that uses concurrency. The problem that I'm having is on the <code>get</code> method, which I can't find any thing wrong with. However, it feels like it is not being accessed correctly, or it's not doing what it is supposed to. The problem, to sum it up, is that I'm getting all the metals, but I'm not giving any to the consumer, which I don't know how that is happening. Let me know if I need to provide any additional information.</p> <p>Each broker maintains a stock of all three metals but is a "supplier" for only one of them, called its "specialty." From time to time, the refiner delivers a shipment of refined metal to the broker who is the supplier for it. For example, the refiner might deliver 30 ounces of gold to the gold supplier. Consumers periodically place purchase orders with brokers. Each order specifies the number of ounces of each metal. It can be placed with any broker. A broker will fill the order from its own stock if possible. If the supplier for metal M cannot fill an order because it does not have enough of M on hand, it just waits until it gets more from the refiner. But if it is short some other metal, it attempts to get it by trading with the supplier for that metal. To keep things simple, we will assume, somewhat unrealistically, that ounces of gold, platinum, or uranium are all equally valuable. That is, three ounces of gold can be swapped for three ounces of uranium or three ounces of platinum.</p> <p>Sorry I can't show the classes that use the BrokerImplementation. I can't because they are all .class files, and didn't think that uploading the bit code would be of any help.</p> <p>Thanks in advance for any help provided.</p> <pre><code>// 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 &lt; 3; i++){ if (metals[i] &gt; 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] &gt;= 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; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload