gpt4 book ai didi

java - 堆栈无法使用 pop 会出现错误

转载 作者:行者123 更新时间:2023-12-01 19:56:40 24 4
gpt4 key购买 nike

我正在尝试获取工作人员执行的每项工作所需的资源。但是我可以从堆栈中获取作业,但我似乎无法获取我尝试过的资源resourceStack.pop(jobres);但似乎并没有让我到任何地方,它只是给出了错误。我需要知道需要多少资源以及每个作业使用的资源。这是我收到的错误 98[LResource;@16f1d982。

worker.java

public class Worker implements Runnable {

private final int id; // Unique worker ID.

private final JobStack jobStack; // Reference to the job stack.
private final ResourceStack resourceStack; // Reference to the resource stack.

private Job job; // Job being processed.
private Resource[] resources; // Resources being used for job being processed.

private boolean busy; // Indicates the status of the worker. True when they are working (executing jobs) and false when there are no more jobs left to execute.

private final Map<Integer, ArrayList<Integer>> jobsCompleted; // The job record of the worker. Stores each job's ID and the IDs of the resources used for each job.

// Constructor.
public Worker(int theId, JobStack theJobStack, ResourceStack theResourceStack) {
id = theId;
jobStack = theJobStack;
resourceStack = theResourceStack;
job = null;
busy = true;
jobsCompleted = new TreeMap<>();
}

/// UNDER CONSTRUCTION /////////////////////////////////////////////////////
public void run() {

ArrayList<Integer> jobId = new ArrayList<Integer>();


try {
System.out.println("Worker " + id + " started Work ");
Thread.sleep(100);
} catch (Exception e) {
System.out.println("Workers starting to work is caught");
}
for (Job job = jobStack.pop(); job != null; job = jobStack.pop()) {
try {
int jobres = job.getResourceRequirement();
System.out.println("Worker " + id + " started Job " + job.getId());
jobId.add(job.getId());
jobsCompleted.put(id, jobId);
Thread.sleep(job.getTimeToComplete());

} catch (Exception e) {
System.out.println("Workers getting jobs is caught");
}
}



if (busy = true) {
System.out.println("Worker " + id + " Finished Work ");
}

}
}

资源.java

public class Resource {

private final int id;

public Resource(int theId) {
id = theId;
}

public int getId() {
return id;
}

}

ResourceStack.java

public class ResourceStack {

private final LinkedList<Resource> stack;
private int resourceCount = 0; // To generate each resource's ID.

private final Lock resourceStackChangeLock;
private final Condition sufficientResourcesCondition;


public ResourceStack(int size) {
stack = new LinkedList<>();
for(int i=0; i<size; i++) {
stack.push(new Resource(resourceCount));
resourceCount++;
}

resourceStackChangeLock = new ReentrantLock();
sufficientResourcesCondition = resourceStackChangeLock.newCondition();
}

public void push(Resource[] resources) {
resourceStackChangeLock.lock();
try {
for (Resource r : resources) {
stack.push(r);
}
sufficientResourcesCondition.signalAll();
} finally {
resourceStackChangeLock.unlock();
}
}

public Resource[] pop(int numberOfRequiredResources) {
Resource[] requiredResources = new Resource[numberOfRequiredResources];
resourceStackChangeLock.lock();
try {
while(stack.size() < numberOfRequiredResources) {
sufficientResourcesCondition.await();
}
for(int i=0; i<numberOfRequiredResources; i++) {
requiredResources[i] = stack.pop();
}
} catch(InterruptedException e) {
System.out.println(e.toString());
} finally {
resourceStackChangeLock.unlock();
}
return requiredResources;
}

public int getSize() {
int returnValue = 0;
resourceStackChangeLock.lock();
try {
returnValue = stack.size();
} finally {
resourceStackChangeLock.unlock();
}
return returnValue;
}

}

最佳答案

尝试在 Ressource.java 类中实现 toString() 方法。

关于java - 堆栈无法使用 pop 会出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49450647/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com