Java线程工作者

时间:2018-03-22 18:39:34

标签: java multithreading netbeans

我正在尝试运行workerThread以便所有工作人员都开始工作我已经在劳动力start()上添加了我的代码但是当我运行它时,它按顺序从0到19给出它们所以我在那里做错了当线程启动时,它通常不会按顺序给它们,我已经尝试删除workerThread i = new thread并放入system.out.println(" worker" + workerThread [i]"而不是它给出了工作线程的结果[Thread-0,5,main]。我怎样才能做得更好。

Worker.java

import java.util.ArrayList;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;

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 ("Thread " + id +" is running");

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

}

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

0 个答案:

没有答案