Logger Al
Logger Al

Reputation: 3

Stack cant use pop gives error

I am trying to get resources needed for each of the job that workers executing. however i can get the jobs out of the stack but i cant seem to get the resources i have tried resourceStack.pop(jobres); but doesnt seem to get me anywhere it just gives error. I need to know how many resources required and the resources each job uses. This is the error i get 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;
}

}

Upvotes: 0

Views: 66

Answers (1)

Bilal EL CHAMI
Bilal EL CHAMI

Reputation: 414

Try to implement toString() method in your Ressource.java class.

Upvotes: 3

Related Questions