Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9307193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T00:20:27+00:00 2026-06-19T00:20:27+00:00

I need help with a java assignment that uses concurrency. The problem that I’m

  • 0

I need help with a java assignment that uses concurrency. The problem that I’m having is on the get 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.

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.

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.

Thanks in advance for any help provided.

// 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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T00:20:28+00:00Added an answer on June 19, 2026 at 12:20 am

    Looking specifically at the get(int[] order) method there are several things that you haven’t considered. Since you mentioned multithreading in your question I assume that multiple threads could be calling this method all at one time. Given this fact, you have not factored in synchronized access to the shared resource metals[]. The operation -= is not thread safe nor is your iteration over that array. Also, when you do add synchronization, attempt to minimize what you synchronize if you are concerned with performance as synchronizing large blocks on this can have an impact on performance.

    EDIT: I also suggest (although you didn’t ask for this) that you do not have a wait() in your synchronized block. This will cause a deadlock as the thread within the synchronized block will be blocking metals[] while waiting.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need help in writing java code that can connect to a remote UNIX
I need your help in java 1.4. How can I prohibit JFrame decreasing. In
I need help to create keyboard shortcuts in my Java program. As can be
I need a help finding java regex pattern to get one query information from
Iam doing a school assignment in Java, and I need some help to do
Need some regex help in Java. I have a string, which contains text and
I need help on how to return a boolean method in java. This is
I need help getting my java program ready for release. I have a program
I need your help in JAVA (with some sample code if possible) regarding to
I need some help with Java Swing components and its capabilities. I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.