我正在尝试获得工作人员执行的每项工作所需的资源。但是我可以把工作从堆栈中取出但我似乎无法获得资源我已经尝试过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 ");
}
}
}
Resource.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;
}
}
答案 0 :(得分:3)
尝试在toString()
课程中实施Ressource.java
方法。