我正在尝试创建一个线程池,使工作人员和工作岗位得以创建,当他们忙碌时,不应该给他们的工作人员提供工作。到目前为止,我有这个代码,但是当我尝试运行它时,因为我有2个尝试并且在worker.java中运行run()它不会按顺序输出它们。我希望第一次尝试发生,以便它显示所有工人已经开始工作,然后我想让它说出哪个工人开始工作。
如果有办法使用busy = true;说这个工人很忙,所以把工作交给下一个工人。
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<>();
}
public void run() {
try
{
System.out.println("Worker " + id + " started Work ");
Thread.sleep(100);
}
catch (Exception e)
{
System.out.println ("Exception is caught");
}
for (Job job = jobStack.pop(); job != null; job = jobStack.pop()) {
try{
System.out.println("Worker " + id + " started Job " + job.getId());
}
catch (Exception e)
{
System.out.println ("Exception is caught");
}
}
}
}
Workforce.java
import java.util.logging.Level;
import java.util.logging.Logger;
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 :(得分:0)
您需要在第一个和第二个try-catch块之间暂停工作线程,并在管理器完成初始化后再次恢复。您可以通过wait()/notify()
实现此目的,这些wait()
旨在阻止(释放)一个线程,直到满足特定条件为止。
我的设计在我看来基于 manager-worker(主从)并行架构。 经理在开始时启动所有工人,之后经理告诉所有工人开始工作。在工作方面,在初始化之后,它立即切换到暂停状态并等待来自管理器的下一个命令。
在经理 - 工作者模型中,通常,经理维护工作池,一组工作项,并且根据请求,经理将下一个可用工作项发送给工人。每个工人一次处理一个工作项,完成后, 请求经理提供新的工作项目。这一直持续到没有剩余工作项目为止。当所有工作项目完成后,经理会告诉工人停止工作。
一种可能的解决方案是在经理和工人之间共享一个共同的锁。在您的第一个try-catch块之后,所有工作人员在该锁上调用notifyAll()
,并且当所有工作程序初始化时,管理器在同一个锁上调用final Object lock = new Object();
。
boolean isWorking = false;
// your first try-catch block
synchronized (lock) {
try {
while (!isWorking) {
lock.wait();
}
isWorking = true;
}
catch (Exception e) {
e.printStackTrace();
}
}
// your second try-catch block
// do your work here!
<强> Wroker 强>
// Starts all the worker threads.
synchronized (lock) {
lock.notifyAll(); // notify workers that initialization is done!
}
<强>管理器强>
find . -name "*.mp4" -print0 | xargs -0 -I $ bash -c "ffmpeg -v error -xerror -i $ -f null - || echo $"