【问题标题】:Java Stack pop() method in Thread线程中的 Java Stack pop() 方法
【发布时间】:2018-09-01 11:35:42
【问题描述】:

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

劳动力.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");
    }

   }
  }

【问题讨论】:

  • 您的代码还不够完整,无法进行测试。您实际上并没有启动任何线程(您从不调用您的 start() 方法。即使您什么都不做也不会发生,因为您的工作人员从不尝试从作业堆栈中获取作业。

标签: java multithreading netbeans linked-list stack


【解决方案1】:

你没有提到什么是 Job 类。所以我只能假设 Job 类有一些方法,其中之一是 Job.getId()。下一个假设是 getId() 返回 Job() 构造函数的 jobCunt 参数。那么这应该可以工作:

public void run() {
    for (Job job = jobStack.pop(); job != null; job = jobStack.pop()) {
        try
        {
            System.out.println ("Worker " + id +" started job  ");
            job.doJob();   
            System.out.println("Worker " + id + " started job " + job.getId());
        }
        catch (Exception e)
        {            
            System.out.println ("Exception is caught");
        }
     }
   }
  }

【讨论】:

  • 一切都好,但我现在被困在资源部分,我怎么能像工作一样做资源.pop();
猜你喜欢
  • 2011-03-16
  • 2012-09-30
  • 2019-04-14
  • 2012-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-18
相关资源
最近更新 更多