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

  • Home
  • SEARCH
  • 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 6878467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:43:38+00:00 2026-05-27T04:43:38+00:00

I have this resource allocator class public class ResourceAllocator { ArrayList<Request> queue = new

  • 0

I have this resource allocator class

public class ResourceAllocator {

ArrayList<Request> queue = new ArrayList<>();
Lock lock = new ReentrantLock();
int maxResources;
int available;

public ResourceAllocator(int max) {
    maxResources = max;
    available = max;
}

public int getMax() {
    return maxResources;
}

public void getResources(Request req) {
    lock.lock();
    try {
        if (req.getRequest() <= available) {
            available = available - req.getRequest();
            req.allocate();
        } else {
            queue.add(req);
        }
    } finally {
        lock.unlock();
    }
}

public void returnResources(int n) {
    lock.lock();
    try {
        available = available + n;
        if (queue.size() > 0) {
            Request req = queue.get(0);
            while (queue.size() > 0 && 
                    req.getRequest() <= available) {
                available = available - req.getRequest();
                req.allocate();
                queue.remove(0);
                if (queue.size() > 0) {
                    req = queue.get(0);
                }
            }
        }
    } finally {
        lock.unlock();
    }
}

public int size(){
    return queue.size();
}
}

which is called from a thread

public class QThread extends Thread {

Semaphore sem = new Semaphore(0);
ResourceAllocator resources;
int number;

public QThread(ResourceAllocator rs, int n) {
    resources = rs;
    number = n;
}

public void run() {
    int items = (int) (Math.random() * resources.getMax()) + 1;
    Request req = new Request(sem, items);
    resources.getResources(req);
    try {
        sem.acquire();
    } catch (InterruptedException ex) {
    }
    System.out.printf("Thread %3d got %3d resources\n", number, items);
    try{
        Thread.sleep(2000);
    }catch(InterruptedException ex){

    }
    resources.returnResources(items);
    System.out.printf("Thread %3d returned %3d resources\n", number,items);
}
}

And all is fine apart from the fact that resources are allocated FIFO.

Any ideas how could I change this to allow clients with small requests to proceed before clients with large requests, bounded overtaking?

  • 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-05-27T04:43:39+00:00Added an answer on May 27, 2026 at 4:43 am

    You can use PriorityQueue which suits best to your needs, then you can implement custom Comparator (if you think in future you may need a different implementation for sorting) or else Comparablewhich will sort your Request so that jobs in terms of size are submitted, executed first.

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

Sidebar

Related Questions

I have this mapper defined: mapper(Resource, resource_table, properties = {'type' : relation(ResourceType,lazy = False),
I have been trying to find some resource on this topic for a while
If I have a Resource bundle property file: A.properties: thekey={0} This is a test
I have a ComponentResourceKey defined in my resource dictionary like this: <Style x:Key={ComponentResourceKey TypeInTargetAssembly={x:Type
I have an assembly that contains an embedded resource. However, when this assembly is
I have some xml similar to this: <?xml version=1.0 encoding=utf-8 ?> <data> <resources> <resource
I have class Person as following : class Person { char* name; int age;
Here is the class structure I'd like to have: public class AllocationNEW<TActivity, TResource> where
I have this code in my View.. <tr><td>Account:</td><td><%=Html.DropDownList(drdAccounts,Model.AccountsListHeader),Model.selectedAccount,Select Account, new { onchange = JavaScript:AccountChanged()
I have this line in my program : InputStream Resource_InputStream=this.getClass().getClassLoader().getResourceAsStream(Resource_Name); But how can I

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.