Java中的资源分配器
资源分配器类
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();
}
}
我有一个从线程调用的
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);
}
}
,除了资源是按 FIFO 分配的之外,一切都很好。
有什么想法我该如何改变这一点,以允许小请求的客户在有大请求的客户之前继续进行,有界超车?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用最适合您需求的
PriorityQueue
,然后您可以实现自定义Comparator
(如果您认为将来可能需要不同的实现来进行排序),或者Comparable
它将对您的Request
进行排序,以便首先提交并执行大小方面的作业。You can use
PriorityQueue
which suits best to your needs, then you can implement customComparator
(if you think in future you may need a different implementation for sorting) or elseComparable
which will sort yourRequest
so that jobs in terms of size are submitted, executed first.使用优先级与请求大小成反比的 PriorityQueue 怎么样?
how about using a PriorityQueue where the priority is the inverse of the size of the request?
如果您提前知道作业的大小,请使用 PriorityQueue 而不是 ArrayList 来保存作业并实现
Comparable
子您的
Request
对象,以便小作业在大作业之前排序。If you know the size of the job ahead of time, use a PriorityQueue instead of an ArrayList to hold the jobs and implement
Comparable
son your
Request
object such that small jobs are sorted before large ones.