线程

时间:2018-03-22 21:21:40

标签: java multithreading netbeans linked-list stack

我有这样的决定,我想尝试让所有工人开始工作,然后每个工人都得到了身份和工作的配合。到目前为止,我已经设法启动了所有工作人员,但我无法将他们与工作分配,我已经尝试过jobStack.pop();希望这会让工作人员工作,但输出Job@6577e002这不是什么我在寻找。所以我希望它说工人3开始工作,工人3完成工作23.我需要做什么才能得到这样的结果。

Workforce.java

public class Workforce {

private final Worker[] pool;  // The worker population.
private int workerCount = 0;  // Used to generate each worker's ID and to keep a record of the number of workers in the workforce.

Thread[] workerThreads;

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

// Constructor.
public Workforce(int size, JobStack theJobStack, ResourceStack theResourceStack) {
    jobStack = theJobStack;
    resourceStack = theResourceStack;

    pool = new Worker[size];
    for(int i=0; i<pool.length; i++) {
        pool[i] = new Worker(workerCount, jobStack, resourceStack);
        workerCount++;
    }

    workerThreads = new Thread[pool.length];
    for(int i=0; i<workerThreads.length; i++) {
        workerThreads[i] = new Thread(pool[i]);
    }
}


/// UNDER CONSTRUCTION /////////////////////////////////////////////////////


// Starts all the worker threads.
public void start() {

    for(int i=0; i<pool.length; i++) {
       workerThreads[i] = new Thread(pool[i]);           
        workerThreads[i].start();
    }

    }

// Checks whether all workers have finished.
public boolean allWorkersFinished() {
    return false;
}

// Prints the job record of all workers.
public void printJobRecords() {
    ;
}
}

Job Stack.java

public class JobStack {

private final int MAX_TIME_REQUIREMENT_PER_JOB = 500;  // Milliseconds
private final int MAX_RESOURCE_COUNT_PER_JOB = 10;

private final LinkedList<Job> stack; 
private int jobCount = 0;  // To generate each job's ID.

private final Lock jobStackChangeLock;

public JobStack(int size) {
    stack = new LinkedList<>();
    Random rn = new Random(12345);  // Created with a seed so that random numbers generated are the same in each run.
    for(int i=0; i<size; i++) {
        stack.push(new Job(jobCount, (rn.nextInt(MAX_TIME_REQUIREMENT_PER_JOB)+1), (rn.nextInt(MAX_RESOURCE_COUNT_PER_JOB)+1)));
        jobCount++;
    }

    jobStackChangeLock = new ReentrantLock();
}

// Will return the next job on the stack or null to signal that there are no more jobs left on the stack.
public Job pop() {
    Job returnValue = null;
    jobStackChangeLock.lock();
    try {
        if(!stack.isEmpty()) {
            returnValue = stack.pop();
        }
    } finally {
        jobStackChangeLock.unlock();
    }
    return returnValue;
}

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

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() {

    try
    {
        System.out.println ("Worker " + id +" started job  ");

    }
    catch (Exception e)
    {
        // Throwing an exception
        System.out.println ("Exception is caught");
    }

   }
  }

1 个答案:

答案 0 :(得分:-1)

你没有提到Job课程的内容。所以我只能假设Job类有一些方法,其中一个是Job.getId()。接下来的假设是getId()返回Job()构造函数的jobCunt参数。然后这应该工作:

%